//
//  ViewController.m
//  TimelineTemp
//
//  Created by Hobo.Chen on 2/14/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "ViewController.h"

#import "UnitView.h"    

#import "PublicMethodEngine.h"
#import "PublicDatabaseEngine.h"
#import "CJSONScanner.h"
#import "CJSONDeserializer.h"

#import "SDDataCache.h"

static float scrolValue = 1;
static float lastScrolValue=1;

static float unitViewWidth =20;

static int  lineCount =60;

static  long long int rulerBeginFlag;

@implementation ViewController

@synthesize unitCategory;
@synthesize lastCategory;

@synthesize diaryIdArray;

@synthesize delegate;
@synthesize userId;
@synthesize diaryId;
@synthesize timelineId;
@synthesize timelineDataDict;

@synthesize rulerDataArray;

@synthesize dyear;
@synthesize dmonth;
@synthesize dday;

@synthesize curDay;
@synthesize curYear;
@synthesize curMonth;

@synthesize myScrollView;
@synthesize scrollBaseView;

@synthesize myTableView;

@synthesize scrollSubViews;

@synthesize indexUp;
@synthesize indexDown;

@synthesize haveIndexUp;
@synthesize haveIndexDown;

@synthesize needIndexUp;
@synthesize needIndexDown;

@synthesize begindate;
@synthesize enddate;
@synthesize currentTime;

@synthesize _diaryEnddate;
@synthesize _diaryBegindate;

@synthesize  _isRightRefresh;
@synthesize _isLeftRefresh;
@synthesize _isFirstIn;

-(id)init{
    
    if([[NSUserDefaults standardUserDefaults] valueForKey:@"timelineId"]){
        
    self.timelineId = [[[NSUserDefaults standardUserDefaults] valueForKey:@"timelineId"] intValue];
        
    }
    
    [super init];
    return self;
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Release any cached data, images, etc that aren't in use.
}

-(void)dealloc{
    
    [diaryIdArray release];
    [lastCategory release];
    [unitCategory release];
    [rulerDataArray release];
    [timelineDataDict release];
    [super dealloc];
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    
    [super viewDidLoad];
    
     [LoadingView showDefaultLoadingView];
    
    
    //初始划 刻度单位
    unitCategory=@"hour";
    scrolValue=2;
    lineCount=40;

    self.userId=[[[NSUserDefaults standardUserDefaults] valueForKey:@"userId"] intValue];
   
    _isFirstIn = YES;
    
    
    //生成尺子
    [self setTimelineRuler];
    
   
    
    [self.view setUserInteractionEnabled:YES];
    [self.view setMultipleTouchEnabled:YES];
    
    [myScrollView setUserInteractionEnabled:YES];
    [myScrollView setMultipleTouchEnabled:YES];
    
    myScrollView.showsHorizontalScrollIndicator=NO;
    myScrollView.maximumZoomScale=1.5;
    myScrollView.minimumZoomScale=0.5;
    
    [scrollBaseView setUserInteractionEnabled:YES];
    
    if (scrollSubViews != nil) {
        [scrollSubViews removeAllObjects];
    }
    else{
        scrollSubViews = [[NSMutableDictionary alloc] init];
    }
    
    //获取当前时间
    NSTimeInterval time = [[NSDate date] timeIntervalSince1970];
    currentTime = (long long int)time; 
    
    if (unitCategory == @"min") {
        
         begindate=currentTime-86400*7; //a week 
        begindate = begindate*1000;

    }
       
    else if(unitCategory == @"hour"){
        
         begindate=currentTime-86400*7; //a week 
        begindate = begindate*1000;
        
    }
    
    else if(unitCategory == @"day"){
        
        begindate=currentTime-86400*30; //a month 
        begindate = begindate*1000;
        
    }
    
    else if(unitCategory == @"month"){
        
        begindate=currentTime-86400*30*30; //30 month 
        begindate = begindate*1000;
        
    }
         
    enddate = currentTime*1000; 
    
    //时间尺起始点标记
    rulerBeginFlag = begindate;
         
    NSString *enddateStr = [NSString stringWithFormat:@"%lld", enddate];
    NSString *begindateStr = [NSString stringWithFormat:@"%lld", begindate];
    
    [self loadRulerDataWith:begindateStr endtime:enddateStr];
    
    
}

#pragma mark 点击时间轴的button事件 

-(void)toDiaryView:(id)sender{
    
    UIButton *button=(UIButton *)sender;

    
    
    NSMutableArray *_diaryIdArray = [[NSMutableArray alloc]init];
    NSMutableArray *_rulerDataArray = [[NSMutableArray alloc]init];
    
    int index;
    index = [diaryIdArray indexOfObject:[NSNumber numberWithInt:button.tag]];
    
//    for (int j=0;j<diaryIdArray.count;j++) {
//        if ([[diaryIdArray objectAtIndex:j] intValue]== button.tag) {
//            
//            index = j;
//            break;
//        }
//                
//    }
    
    
    
    for (int i = 0; i <= index; ++i) {
        [_diaryIdArray addObject:[diaryIdArray objectAtIndex:i ]];
        [_rulerDataArray addObject:[rulerDataArray objectAtIndex:i ]];

    }
  
    _isFirstIn = YES;
    [self loadLastDiary:_diaryIdArray rulerDataArray:_rulerDataArray];
    _isFirstIn = NO;
    
//    if (self.delegate!=nil) {
//        [self.delegate  loadRecordData:button.tag];
//    }
    
}



- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
	[super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated
{
	[super viewDidDisappear:animated];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
        return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
    } else {
        return YES;
    }
}
  

#pragma mark Table View

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 100;
}

// Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:
// Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
	
	NSInteger row = [indexPath row];
	NSInteger section = [indexPath section];
	static NSString *Identifier = @"NoteListCellView";
	static NSString *MyIdentifier = @"MyIdentifier";
	
	// Try to retrieve from the table view a now-unused cell with the given identifier.
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
    
    // If no cell is available, create a new one using the given identifier.
    if (cell == nil) {
        // Use the default cell style.
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier] ;
    }			
			
			
    [cell setBackgroundColor:[UIColor clearColor]];
			
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
			
    return cell;

	
    
}




- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"Touches began!");
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"Touches move!");
    
    UITouch *touch = [touches anyObject];
    
    CGPoint prePoint = [touch previousLocationInView:myScrollView];
    CGPoint curPoint = [touch locationInView:myScrollView];
    
}

#pragma  mark -Load data
#pragma mark -下载尺子数据

-(void)loadRulerDataWith:(NSString *)begintime endtime:(NSString *)endtime{
    
    NSString *str=[NSString stringWithFormat:@"%d&beginDate=%@&endDate=%@",self.timelineId,begintime,endtime];
    
    NSMutableURLRequest * request = [WebRequest diaryTLRuleRequestWith:str];
	
	CLog(@"request %@", request);
	WebConnectionArray *connection = [[WebConnectionArray alloc] initWith:request delegate:self dataBaseListName:nil];
	
   // [LoadingView showDefaultLoadingView];
    
}


#pragma mark 
#pragma mark 尺子reload

- (void)reloadRulerData{
    
    int _min;
    int _hour;
    int _day;
    int _month;
    int _year;
            
        for (int i = indexDown; i <= indexUp; i++) {
            
            NSString *unitViewXPosition = [NSString stringWithFormat:@"unit%d", i];
            
            UIView *unitView = [scrollSubViews objectForKey:unitViewXPosition]; 
            
            
            if(unitCategory==@"min"){
                               
                _min = 60-i%60;
                if (_min == 60) {
                    _min=0;
                }
                
                _hour = 23-((i-1)/60)%24;
                
                _day = abs(self.curDay-i/1440)%30;
                if (_day == 0) {
                    _day = 30;
                }
                
                if (self.curDay-i/1440<0) {
                    _day = 30-abs(self.curDay-i/1440)%30;
                }
                
                    
                _month = abs(curMonth-i/43200)%12;
                
                if (_month == 0) {
                    _month = 12;
                }
                if (curMonth-i/43200<0) {
                    _month = 12-abs(curMonth-i/43200)%12;
                }
                _year = curYear-i/518400;
                
                NSString *yearKey = [NSString stringWithFormat:@"year%d", _year];
                NSMutableDictionary *yearDict = [self.timelineDataDict objectForKey:yearKey];
                if (yearDict != nil) {
                    
                    NSString *monthKey = [NSString stringWithFormat:@"month%d", _year];
                    if (_month<10) {
                        monthKey=[NSString stringWithFormat:@"month0%d", _month];
                    }
                    NSMutableDictionary *monthDict = [yearDict objectForKey:monthKey];
                    if (monthDict != nil) {
                        
                        NSString *dayKey = [NSString stringWithFormat:@"day%d", _day];
                        if (_day<10) {
                            dayKey=[NSString stringWithFormat:@"day0%d", _day];
                        }
                        NSMutableDictionary *dayDict = [monthDict objectForKey:dayKey];
                        if (dayDict != nil) {
                            
                            NSString *hourKey = [NSString stringWithFormat:@"hour%d", _hour];
                            if (_hour<10) {
                                hourKey = [NSString stringWithFormat:@"hour0%d", _hour];
                                
                            }
                            
                            NSMutableDictionary *hourDict = [dayDict objectForKey:hourKey];
                            if (hourDict != nil) {
                                
                                NSString *minKey = [NSString stringWithFormat:@"min%d", _min];
                                if (i<10) {
                                    minKey=[NSString stringWithFormat:@"min0%d", _min];
                                }
                                NSMutableDictionary *minDict = [hourDict objectForKey:minKey];
                                if (minDict != nil) {
                                    if([[minDict objectForKey:@"diarys"] count]!=0)   {
                                        
                                        UIButton *button=[[UIButton alloc]initWithFrame:CGRectMake(unitView.frame.origin.x-unitView.frame.size.width/4, unitView.frame.origin.y, unitView.frame.size.width/2, unitView.frame.size.height)];                                        
                                        button.tag=[[[[minDict objectForKey:@"diarys"] objectAtIndex:0] objectForKey:@"id"] intValue];
                                        
                                        
                                        button.userInteractionEnabled=YES;
                                        [button addTarget:self action:@selector(toDiaryView:) forControlEvents:UIControlEventTouchUpInside];
                                        [button setBackgroundColor:[UIColor clearColor]];
                                        UIImageView *poitImageView = [[UIImageView alloc] initWithFrame:CGRectMake(unitViewWidth/2-12, unitView.frame.size.height/2-10, 6, 6)];
                                        [poitImageView setImage:[UIImage imageNamed:@"point_3.png"]];
                                        //[button addSubview:poitImageView];
                                        [unitView  addSubview:poitImageView];
                                        [poitImageView release];
                                        
                                        [button setCenter:CGPointMake((unitViewWidth/2)*i*scrolValue, (100 -40) /2)];
                                        [myScrollView addSubview:button];
                                        [scrollSubViews setObject:button forKey:[NSString stringWithFormat:@"button%d",i]];
                                        [button release];
                                        
                                        
                                    }
                                    
                                }
                                
                            }
                            
                            
                        }
                        
                        
                    }
                    
                    
                }
   }//category
            
            
            
            if(unitCategory==@"hour")
            {
                
                
                _hour=24-i%24;
                if (_hour==24) {
                    _hour=0;
                }
                
                _day=abs(self.curDay-(i-1)/24)%30;
                if (_day == 0) {
                    _day = 30;
                }
                if (self.curDay-i/24<0) {
                    _day = 30-abs(self.curDay-(i-1)/24)%30;
                }
                
                _month = abs(curMonth-i/720)%12;
                if (_month == 0) {
                    _month = 12;
                    
                }
                if (curMonth-i/720<0) {
                    _month = 12-abs(curMonth-i/720)%12;
                }
                
                _year = curYear-i/8640;
                
                
                NSString *yearKey = [NSString stringWithFormat:@"year%d", _year];
                NSMutableDictionary *yearDict = [self.timelineDataDict objectForKey:yearKey];
                if (yearDict != nil) {
                    
                    NSString *monthKey = [NSString stringWithFormat:@"month%d", _month];
                    if (_month<10) {
                        monthKey=[NSString stringWithFormat:@"month0%d", _month];
                    }
                    NSMutableDictionary *monthDict = [yearDict objectForKey:monthKey];
                    if (monthDict != nil) {
                        
                        NSString *dayKey = [NSString stringWithFormat:@"day%d", _day];
                        if (_day<10) {
                            dayKey=[NSString stringWithFormat:@"day0%d", _day];
                        }
                        NSMutableDictionary *dayDict = [monthDict objectForKey:dayKey];
                        if (dayDict != nil) {
                            
                            NSString *hourKey = [NSString stringWithFormat:@"hour%d", _hour];
                            if (_hour<10) {
                                hourKey = [NSString stringWithFormat:@"hour0%d", _hour];
                                
                            }
                            
                            NSMutableDictionary *hourDict = [dayDict objectForKey:hourKey];
                            if (hourDict != nil) {
                                
                                
                                if([[hourDict objectForKey:@"diarys"] count]!=0)   {
                                
                                    
                                    UIButton *button=[[UIButton alloc]initWithFrame:CGRectMake(unitView.frame.origin.x-unitView.frame.size.width/4, unitView.frame.origin.y, unitView.frame.size.width/2, unitView.frame.size.height)];
                                    
                                    button.tag=[[[[hourDict objectForKey:@"diarys"] objectAtIndex:0] objectForKey:@"id"] intValue];
                                    
                                    
                                    button.userInteractionEnabled=YES;
                                    [button addTarget:self action:@selector(toDiaryView:) forControlEvents:UIControlEventTouchUpInside];
                                    [button setBackgroundColor:[UIColor clearColor]];
                                    UIImageView *poitImageView = [[UIImageView alloc] initWithFrame:CGRectMake(unitViewWidth/2-12, unitView.frame.size.height/2-10, 6, 6)];
                                    [poitImageView setImage:[UIImage imageNamed:@"point_3.png"]];
                                   // [button addSubview:poitImageView];
                                    [unitView addSubview:poitImageView];
                                    [poitImageView release];
                                    
                                    
                                    [button setCenter:CGPointMake((unitViewWidth/2)*i*scrolValue, (100 -40) /2)];
                                    [myScrollView addSubview:button];
                                    [scrollSubViews setObject:button forKey:[NSString stringWithFormat:@"button%d",i]];
                                    [button release];
                                    
                                    
                                }
                                
                            }
                            
                            
                        }
                        
                        
                    }
                    
                    
                }
               
            }//category
            
            
            if(unitCategory==@"day")
            {
                
                _day = 30-i%30;
                if (_day == 0) {
                    _day = 30;
                }
                
                _month = abs(curMonth-i/30)%12;
                if (_month == 0) {
                    _month = 12;
                }
                if (curMonth-i/30<0) {
                    _month = 12-abs(curMonth-i/30)%12;
                }
                
                _year = curYear-i/360;
                
                NSString *yearKey = [NSString stringWithFormat:@"year%d", _year];
                NSMutableDictionary *yearDict = [self.timelineDataDict objectForKey:yearKey];
                if (yearDict != nil) {
                    
                    NSString *monthKey = [NSString stringWithFormat:@"month%d", _month];
                    if (_month <10) {
                        monthKey=[NSString stringWithFormat:@"month0%d", _month];
                    }
                    NSMutableDictionary *monthDict = [yearDict objectForKey:monthKey];
                    if (monthDict != nil) {
                        
                        NSString *dayKey = [NSString stringWithFormat:@"day%d", _day];
                        if (_day<10) {
                            dayKey=[NSString stringWithFormat:@"day0%d", _day];
                        }
                        NSMutableDictionary *dayDict = [monthDict objectForKey:dayKey];
                        if (dayDict != nil) {
                            
                            
                            if([[dayDict objectForKey:@"diarys"] count]!=0)   {
                                
                                
                                UIButton *button=[[UIButton alloc]initWithFrame:CGRectMake(unitView.frame.origin.x-unitView.frame.size.width/4, unitView.frame.origin.y, unitView.frame.size.width/2, unitView.frame.size.height)];                                
                                button.tag=[[[[dayDict objectForKey:@"diarys"] objectAtIndex:0] objectForKey:@"id"] intValue];
                                
                                
                                button.userInteractionEnabled=YES;
                                [button addTarget:self action:@selector(toDiaryView:) forControlEvents:UIControlEventTouchUpInside];
                                [button setBackgroundColor:[UIColor clearColor]];
                                UIImageView *poitImageView = [[UIImageView alloc] initWithFrame:CGRectMake(unitViewWidth/2-12, unitView.frame.size.height
/2-10, 6, 6)];
                                [poitImageView setImage:[UIImage imageNamed:@"point_3.png"]];
                               // [button addSubview:poitImageView];
                                [unitView addSubview:poitImageView];
                                [poitImageView release];
                                
                                [button setCenter:CGPointMake((unitViewWidth/2)*i*scrolValue, (100 -40) /2)];
                                [myScrollView addSubview:button];
                                [scrollSubViews setObject:button forKey:[NSString stringWithFormat:@"button%d",i]];
                                [button release];
                                
                                
                            }
                            
                        }
                        
                        
                    }
                    
                    
                }
            
            }//category
            
            if(unitCategory==@"month")
            {
                
                _month = 12-i%12;
                if (_month == 0) {
                    _month = 12;
                }
                if (i<0) {
                    _month = 12-abs(i)%12;
                }
                _year = curYear-i/12;
                
                NSString *yearKey = [NSString stringWithFormat:@"year%d", _year];
                NSMutableDictionary *yearDict = [self.timelineDataDict objectForKey:yearKey];
                if (yearDict != nil) {
                    
                    NSString *monthKey = [NSString stringWithFormat:@"month%d", _month];
                    if (_month<10) {
                        monthKey=[NSString stringWithFormat:@"month0%d", _month];
                    }
                    NSMutableDictionary *monthDict = [yearDict objectForKey:monthKey];
                    if (monthDict != nil) {
                        
                        UIButton *button=[[UIButton alloc]initWithFrame:CGRectMake(unitView.frame.origin.x-unitView.frame.size.width/4, unitView.frame.origin.y, unitView.frame.size.width/2, unitView.frame.size.height)];
                        
                        button.tag=[[[[monthDict objectForKey:@"diarys"] objectAtIndex:0] objectForKey:@"id"] intValue];
                        
                        
                        button.userInteractionEnabled=YES;
                        [button addTarget:self action:@selector(toDiaryView:) forControlEvents:UIControlEventTouchUpInside];
                        [button setBackgroundColor:[UIColor clearColor]];
                        UIImageView *poitImageView = [[UIImageView alloc] initWithFrame:CGRectMake(unitViewWidth/2-12, unitView.frame.size.height/2-10, 6, 6)];
                        [poitImageView setImage:[UIImage imageNamed:@"point_3.png"]];
                       // [button addSubview:poitImageView];
                        [unitView addSubview:poitImageView];
                        [poitImageView release];
                        
                        
                        [button setCenter:CGPointMake((unitViewWidth/2)*i*scrolValue, (100 -40) /2)];
                        [myScrollView addSubview:button];
                        
                        [scrollSubViews setObject:button forKey:[NSString stringWithFormat:@"button%d",i]];
                        [button release];
                        
                        
                    }
                    
                    
                }
               
            }
            
            CLog(@"category %@  i %d", unitCategory ,i);
            CLog(@"_year%d _month%d _day%d _hour%d _min%d", _year,_month,_day,_hour,_min);
            
        }
    
}



#pragma mark Timeline Data
#pragma mark 尺子时间字典初始化

- (void)timelineDataInit:(NSArray *)DiaryArray
{
    
    if (self.timelineDataDict==nil) {
        self.timelineDataDict=[[NSMutableDictionary alloc]init];
    }
        
    for (NSDictionary *dict in DiaryArray) {
        
        
        if(dict){
            
            //[dict setValue:[NSNumber numberWithInt:DiaryArray.count-1] forKey:@"buttonId"];
            
                       
            long long int diaryDate=[[dict objectForKey:@"diaryDate"] longLongValue];
            // NSDate  datetime=[time ];
            
            NSDate *nd = [NSDate dateWithTimeIntervalSince1970:diaryDate/1000];
            
            
            NSCalendar *cal = [NSCalendar currentCalendar];
            unsigned int unitFlags =NSYearCalendarUnit| NSMonthCalendarUnit| NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit;
            NSDateComponents *dd = [cal components:unitFlags fromDate:nd];
            int year = [dd year];
            int month=[dd month];
            int day=[dd day];
            int hour = [dd hour];
            int minute = [dd minute];
            
            CLog(@"time %d,%d,%d,%d", month,day,hour,minute);
            
            
            
            if(unitCategory==@"min"){
            NSString *yearKey = [NSString stringWithFormat:@"year%d", year];
            NSMutableDictionary *yearDict = [self.timelineDataDict objectForKey:yearKey];
            if (yearDict == nil) {
                yearDict = [[NSMutableDictionary alloc] init];
                
                [timelineDataDict setObject:yearDict forKey:yearKey];
            }
            
            NSString *monthKey = [NSString stringWithFormat:@"month%d", month];
            if (month<10) {
                monthKey=[NSString stringWithFormat:@"month0%d", month];
            }
            NSMutableDictionary *monthDict = [yearDict objectForKey:monthKey];
            if (monthDict == nil) {
                monthDict = [[NSMutableDictionary alloc] init];
                
                [yearDict setObject:monthDict forKey:monthKey];
            }
            
            NSString *dayKey = [NSString stringWithFormat:@"day%d", day];
            if (day<10) {
                dayKey=[NSString stringWithFormat:@"day0%d", day];
            }
            NSMutableDictionary *dayDict = [monthDict objectForKey:dayKey];
            if (dayDict == nil) {
                dayDict = [[NSMutableDictionary alloc] init];
                
                [monthDict setObject:dayDict forKey:dayKey];
            }
            
            NSString *hourKey = [NSString stringWithFormat:@"hour%d", hour];
            if (hour<10) {
                hourKey=[NSString stringWithFormat:@"hour0%d", hour];
            }
            NSMutableDictionary *hourDict = [dayDict objectForKey:hourKey];
            if (hourDict == nil) {
                hourDict = [[NSMutableDictionary alloc] init];
                
                [dayDict setObject:hourDict forKey:hourKey];
            }
            
            NSString *minKey = [NSString stringWithFormat:@"min%d", minute];
            if (minute<10) {
                minKey=[NSString stringWithFormat:@"min0%d", minute];
            }
            NSMutableDictionary *minDict = [hourDict objectForKey:minKey];
            if (minDict == nil) {
                minDict = [[NSMutableDictionary alloc] init];
                
                [hourDict setObject:minDict forKey:minKey];
            }
            
             if([minDict objectForKey:@"diarys"]==nil){
                 
                  NSMutableArray *diarys = [[NSMutableArray alloc] init] ;
            [minDict setObject:diarys forKey:@"diarys"];
            }
           
            [[minDict objectForKey:@"diarys"] addObject:dict];
           
            
    }
            
            
            if (unitCategory==@"hour") {
                NSString *yearKey = [NSString stringWithFormat:@"year%d", year];
                NSMutableDictionary *yearDict = [self.timelineDataDict objectForKey:yearKey];
                if (yearDict == nil) {
                    yearDict = [[NSMutableDictionary alloc] init];
                    
                    [timelineDataDict setObject:yearDict forKey:yearKey];
                }
                
                NSString *monthKey = [NSString stringWithFormat:@"month%d", month];
                if (month<10) {
                    monthKey=[NSString stringWithFormat:@"month0%d", month];
                }
                NSMutableDictionary *monthDict = [yearDict objectForKey:monthKey];
                if (monthDict == nil) {
                    monthDict = [[NSMutableDictionary alloc] init];
                    
                    [yearDict setObject:monthDict forKey:monthKey];
                }
                
                NSString *dayKey = [NSString stringWithFormat:@"day%d", day];
                if (day<10) {
                    dayKey=[NSString stringWithFormat:@"day0%d", day];
                }
                NSMutableDictionary *dayDict = [monthDict objectForKey:dayKey];
                if (dayDict == nil) {
                    dayDict = [[NSMutableDictionary alloc] init];
                    
                    [monthDict setObject:dayDict forKey:dayKey];
                }
                
                NSString *hourKey = [NSString stringWithFormat:@"hour%d", hour];
                if (hour<10) {
                    hourKey=[NSString stringWithFormat:@"hour0%d", hour];
                }
                NSMutableDictionary *hourDict = [dayDict objectForKey:hourKey];
                if (hourDict == nil) {
                    hourDict = [[NSMutableDictionary alloc] init];
                    
                    [dayDict setObject:hourDict forKey:hourKey];
                }
                
        if([hourDict objectForKey:@"diarys"]==nil){
                NSMutableArray *diarys = [[NSMutableArray alloc] init] ;
             [hourDict setObject:diarys forKey:@"diarys"];
        }
                [[hourDict objectForKey:@"diarys"] addObject:dict];
              
    }     
            
            if(unitCategory==@"day")
            {
                NSString *yearKey = [NSString stringWithFormat:@"year%d", year];
                NSMutableDictionary *yearDict = [self.timelineDataDict objectForKey:yearKey];
                if (yearDict == nil) {
                    yearDict = [[NSMutableDictionary alloc] init];
                    
                    [timelineDataDict setObject:yearDict forKey:yearKey];
                }
                
                NSString *monthKey = [NSString stringWithFormat:@"month%d", month];
                if (month<10) {
                    monthKey=[NSString stringWithFormat:@"month0%d", month];
                }
                NSMutableDictionary *monthDict = [yearDict objectForKey:monthKey];
                if (monthDict == nil) {
                    monthDict = [[NSMutableDictionary alloc] init];
                    
                    [yearDict setObject:monthDict forKey:monthKey];
                }
                
                NSString *dayKey = [NSString stringWithFormat:@"day%d", day];
                if (day<10) {
                    dayKey=[NSString stringWithFormat:@"day0%d", day];
                }
                NSMutableDictionary *dayDict = [monthDict objectForKey:dayKey];
                if (dayDict == nil) {
                    dayDict = [[NSMutableDictionary alloc] init];
                    
                    [monthDict setObject:dayDict forKey:dayKey];
                }
                
                if([dayDict objectForKey:@"diarys"]==nil){
                    
                     NSMutableArray *diarys = [[NSMutableArray alloc] init] ;
                    [dayDict setObject:diarys forKey:@"diarys"];
                }
                
               
                [[dayDict objectForKey:@"diarys"] addObject:dict];
                
            }
            
            if (unitCategory==@"month") {
                NSString *yearKey = [NSString stringWithFormat:@"year%d", year];
                NSMutableDictionary *yearDict = [self.timelineDataDict objectForKey:yearKey];
                if (yearDict == nil) {
                    yearDict = [[NSMutableDictionary alloc] init];
                    
                    [timelineDataDict setObject:yearDict forKey:yearKey];
                }
                
                NSString *monthKey = [NSString stringWithFormat:@"month%d", month];
                if (month<10) {
                    monthKey=[NSString stringWithFormat:@"month0%d", month];
                }
                NSMutableDictionary *monthDict = [yearDict objectForKey:monthKey];
                if (monthDict == nil) {
                    monthDict = [[NSMutableDictionary alloc] init];
                    
                    [yearDict setObject:monthDict forKey:monthKey];
                }
                
                 if([monthDict objectForKey:@"diarys"]==nil){
                     
                      NSMutableArray *diarys = [[NSMutableArray alloc] init] ;

                    [monthDict setObject:diarys forKey:@"diarys"];
                }
                               [[monthDict objectForKey:@"diarys"] addObject:dict];
               
                
            }  
            
            if (unitCategory==@"year") {
                NSString *yearKey = [NSString stringWithFormat:@"year%d", year];
                NSMutableDictionary *yearDict = [self.timelineDataDict objectForKey:yearKey];
                if (yearDict == nil) {
                    yearDict = [[NSMutableDictionary alloc] init];
                    
                    [timelineDataDict setObject:yearDict forKey:yearKey];
                }
                 if([yearDict objectForKey:@"diarys"]==nil){
                     
                      NSMutableArray *diarys = [[NSMutableArray alloc] init] ;

                    [yearDict setObject:diarys forKey:@"diarys"];
                }

                               [[yearDict objectForKey:@"diarys"] addObject:dict];
                               
            }  

            
            
        }
    }
    
   // [self saveTLRulerDataToDatabase];

    
}



#pragma mark -WebConnectionArray delegate

- (void)connectionArraySuccess:(NSDictionary *)dict{
    
    //尺子数据下载成功
    
    //第一次进来把该版块缓存的图片清空
    if(_isFirstIn){
        [[SDDataCache sharedDataCache:kImageTypeTimeline] clearDisk];
        
        
        PublicDatabaseEngine *databaseEngine = [PublicDatabaseEngine sharedDatabaseEngine];
        //获取已存在的时间尺子数据
        NSString *sql1 = [NSString stringWithFormat:@"select * from DiaryViews where id=%d ",self.timelineId];
        
        NSMutableArray *oldArray  =[databaseEngine selectTLRulerWithSQL:sql1];
        
        
        //删除缓存中旧的尺子相应的日志记录
        for (NSDictionary *dict in oldArray) {
            int did = [[dict objectForKey:@"id"] intValue];
            NSString *sql = [NSString stringWithFormat:@"delete from DiaryView where id = %d", did];
            [databaseEngine executeSQL:sql];
            
        }
        
        
        
        
    }
    
      NSArray *array = [dict objectForKey:@"object"];
    
    // 把尺子数据放到时间字典
    [self timelineDataInit:array];
    
    
    if(array.count>0){    
        
        if (self.rulerDataArray == nil) {
            self.rulerDataArray = [[NSMutableArray alloc] init];
        }
       
        
        //尺子数组数据，包括id和时间
        if(_isFirstIn || _isRightRefresh){
            
            NSMutableIndexSet *indexes = [NSMutableIndexSet indexSetWithIndexesInRange:NSMakeRange(0, array.count)];
        [self.rulerDataArray insertObjects:array atIndexes:indexes];
      //  [self.rulerDataArray addObjectsFromArray:array];
        }
        else if(_isLeftRefresh){
            
//             NSMutableIndexSet *indexes = [NSMutableIndexSet indexSetWithIndexesInRange:NSMakeRange(0, array.count)];
//            [self.rulerDataArray insertObjects:array atIndexes:indexes];
            
            NSTimeInterval time = [[NSDate date] timeIntervalSince1970];
            rulerBeginFlag = ((long long int)time-86400*7)*1000; 
            
            [self.rulerDataArray removeAllObjects];
            [self.rulerDataArray addObjectsFromArray:array];
            
        }
        
        //只存放id
        
        if (self.diaryIdArray != nil) {
            [self.diaryIdArray removeAllObjects];
        }
        else{
            self.diaryIdArray=[[NSMutableArray alloc]init];
        }
        
        for (NSDictionary *dictionary in self.rulerDataArray) {
             [diaryIdArray addObject:[dictionary objectForKey:@"id"]];
        }
       
        
        CLog(@"content success!");
        
        
        //第一次进来 或者 左右刷新内容的时候
        if (_isRightRefresh || _isFirstIn ||_isLeftRefresh) {
          
            [self loadLastDiary:self.diaryIdArray rulerDataArray:self.rulerDataArray];
              _isRightRefresh = NO;
            _isFirstIn = NO;
            _isLeftRefresh = NO;
            [LoadingView hideWithAnimated:YES];

            
        }
       

    }
    
    //没有日志  继续往前刷
    else{
        
        if(_isFirstIn){
            
            
            if (begindate < 1300000000000) {
                [LoadingView hideWithAnimated:YES];

                _isFirstIn = NO;
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Faild!" message:@"宝宝这段时间还没有日志记录哦！" 
                                                               delegate:self 
                                                      cancelButtonTitle:@"确定" otherButtonTitles:nil];
                
                [alert show];
                [alert release];
                return;

                
            }
            
            if (unitCategory == @"min" || unitCategory == @"hour") {
                
                begindate = begindate -86400000*7;
                         }
            if (unitCategory == @"day") {
                
                begindate = begindate/1000 -86400*30;
                begindate = begindate*1000;
                
            }
            if (unitCategory == @"month") {
                
                begindate = begindate/1000 -86400*30*30;
                begindate = begindate*1000;
                }
            
            rulerBeginFlag = begindate;
            
            NSString *enddateStr = [NSString stringWithFormat:@"%lld", begindate];
            NSString *begindateStr = [NSString stringWithFormat:@"%lld",enddate];
            [self loadRulerDataWith:begindateStr endtime:enddateStr];
      
        }
        
        if( _isRightRefresh ){
            
            if (_diaryBegindate < 1300000000000) {
                [LoadingView hideWithAnimated:YES];
                _isRightRefresh = NO;
                
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Faild!" message:@"已经是最后一篇日志！" 
                                                               delegate:self 
                                                      cancelButtonTitle:@"确定" otherButtonTitles:nil];
                
                [alert show];
                [alert release];
                return;

            }
            
            
            if (unitCategory == @"min" || unitCategory == @"hour") {
                
                _diaryBegindate = _diaryBegindate -86400000*7;
                              
            }
            if (unitCategory == @"day") {
                
                _diaryBegindate = _diaryBegindate/1000 -86400*30;
                _diaryBegindate = _diaryBegindate*1000;
                                
            }
            if (unitCategory == @"month") {
                
                _diaryBegindate = _diaryBegindate/1000 -86400*30*30;
                _diaryBegindate = _diaryBegindate*1000;
                
            }
            
            rulerBeginFlag = _diaryBegindate;
            
            NSString *enddateStr = [NSString stringWithFormat:@"%lld", _diaryEnddate];
            NSString *begindateStr = [NSString stringWithFormat:@"%lld",_diaryBegindate];
            [self loadRulerDataWith:begindateStr endtime:enddateStr];
            
        }

        
        //左边刷新条
        
        if( _isLeftRefresh){
            
            
            if (begindate < 1300000000000) {
                [LoadingView hideWithAnimated:YES];
                
                _isLeftRefresh = NO;
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Faild!" message:@"宝宝还没有日志记录哦！" 
                                                               delegate:self 
                                                      cancelButtonTitle:@"确定" otherButtonTitles:nil];
                
                [alert show];
                [alert release];
                return;
                
                
            }
            
            if (unitCategory == @"min" || unitCategory == @"hour") {
                
                begindate = begindate -86400000*7;
            }
            if (unitCategory == @"day") {
                
                begindate = begindate/1000 -86400*30;
                begindate = begindate*1000;
                
            }
            if (unitCategory == @"month") {
                
                begindate = begindate/1000 -86400*30*30;
                begindate = begindate*1000;
            }
            
            rulerBeginFlag = begindate;
            
            NSString *enddateStr = [NSString stringWithFormat:@"%lld", begindate];
            NSString *begindateStr = [NSString stringWithFormat:@"%lld",enddate];
            [self loadRulerDataWith:begindateStr endtime:enddateStr];
            
        }

        
    }
    
}

- (void)connectionArrayError:(NSError *)error{
    
    //[self.delegate doneLoadingTableViewData];
    
    
    [self.delegate doneLoadingTableViewData];
    
    //从缓存读取数据
    if(_isFirstIn || scaleIsChange){
    [self  loadTLRulerDataFromDataBase ];
        
    }
        
        
}


#pragma mark -WebConnection delegate

- (void)connectionSuccess:(NSDictionary *)dict{
    
      
    CLog(@"content success!");
    
}

- (void)connectionError:(NSError *)error{
}

- (void)postSuccess:(NSString *)postinfo{
    CLog(@"post info");
    
    
}


#pragma mark -load data from database
-(void)loadTLRulerDataFromDataBase{
    
   
    NSString *sql = [NSString stringWithFormat:@"select * from DiaryViews where id=%d ",self.timelineId];
    
    PublicDatabaseEngine *databaseEngine = [PublicDatabaseEngine sharedDatabaseEngine];
    
    if (self.rulerDataArray != nil) {
        [rulerDataArray removeAllObjects];
    }
    else{
        
        self.rulerDataArray= [[NSMutableArray alloc] init];
    }
    
     NSMutableArray *array  =[databaseEngine selectTLRulerWithSQL:sql];
    
    [self timelineDataInit:array];
	
	[self.rulerDataArray addObjectsFromArray:array];
    
    
    //只存放id
    
    if (self.diaryIdArray != nil) {
        [self.diaryIdArray removeAllObjects];
    }
    else{
        self.diaryIdArray=[[NSMutableArray alloc]init];
    }
    
    for (NSDictionary *dictionary in self.rulerDataArray) {
        [diaryIdArray addObject:[dictionary objectForKey:@"id"]];
    }
    

    
	CLog(@"content success!");
    
    if (_isFirstIn || scaleIsChange) {
         [self setTimelineRuler];
    }
   

    if (_isLeftRefresh || _isRightRefresh || _isFirstIn) {
        
        [self loadLastDiary:self.diaryIdArray rulerDataArray:self.rulerDataArray];
        _isLeftRefresh = NO;
        _isRightRefresh = NO;
        _isFirstIn = NO;
        
    }
    
   
    
}


-(void)loadLastDiary:(NSMutableArray *)_diaryArray rulerDataArray:(NSMutableArray *)_rulerDataArray{
    
       if (self.delegate!=nil) {
           [self.delegate loadLastDiary:_diaryArray rulerDataArray:_rulerDataArray];
    }

    
}


-(void)setTimelineRuler{
    
    if (self.timelineDataDict==nil) {
        self.timelineDataDict=[[NSMutableDictionary alloc]init];
    }
    
    
    if(scaleIsChange){  //刻度改变
        
        for (UIView *view in scrollBaseView.subviews) {
        [view removeFromSuperview];
            
    }
        
    for (UIView *view in myScrollView.subviews) {
        
        if ([view isMemberOfClass:[UIButton class]]) {
                [view removeFromSuperview];
        }
        
    }
        
        [scrollSubViews removeAllObjects];
        
        CLog(@"scale %f", scrolValue);
        
        [myScrollView setZoomScale:1];
        scrollBaseView.transform=CGAffineTransformMakeScale(1, 1);
        scrollBaseView.center = CGPointMake(scrollBaseView.frame.size.width /2, scrollBaseView.frame.size.height /2);
          
     }
   
    
    if(_isFirstIn ||scaleIsChange){
    
        scaleIsChange=NO;
        
        //获取当时时间的年月日整型数
        NSDate *nd = [NSDate date];
        NSCalendar *cal = [NSCalendar currentCalendar];
        unsigned int unitFlags =NSYearCalendarUnit| NSMonthCalendarUnit| NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit;
        NSDateComponents *dd = [cal components:unitFlags fromDate:nd];
        self.dyear = [dd year];
        dmonth=[dd month];
        dday=[dd day];
        dhour = [dd hour];
        dminute = [dd minute];
        
        curYear = [dd year];
        curMonth=[dd month];
        self.curDay=[dd day];

        // 尺子显示起始点
        if (unitCategory==@"min") {
            dhour=23;
            dminute=59;
        }
        if (unitCategory==@"hour") {
            dhour=23;
            
        }
        if (unitCategory==@"day") {
            dday=30;
        }
        if (unitCategory==@"month") {
            dmonth=12;
        }
        
        float height = 0;
        
        for (int i = 1; i <=lineCount; i++) {
             
            if(unitCategory==@"month")
            {
                if (i %3 == 0) {
                    height = 45;
               
                           }
                else{
                    height = 30;
                }
            UnitView *unitView = [[UnitView alloc] initWithFrame:CGRectMake(0, 0, unitViewWidth*scrolValue, height)];
            unitView.tag=10;
            [unitView setCenter:CGPointMake((unitViewWidth/2)*i*scrolValue, (100 -height) /2)];
            [scrollBaseView addSubview:unitView];

            dmonth=12-i%12;
                if (dmonth==0) {
                    dmonth=12;
                }
                if (i<0) {
                    dmonth=abs(i)%12;
                }
                         
                dyear=curYear-i/12;
           
            if (i%3==0) {
                UILabel *danweiLabel=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 20)];
                  
                [danweiLabel setTextColor:[UIColor lightGrayColor]];
                [danweiLabel setTextAlignment:UITextAlignmentCenter];
                [danweiLabel setBackgroundColor:[UIColor clearColor]];
                [danweiLabel.text sizeWithFont:[UIFont systemFontOfSize:1]];
                danweiLabel.text=[NSString stringWithFormat:@"%d年%d月",dyear,dmonth];
                [unitView addSubview:danweiLabel];
                [danweiLabel release];
            }
                      
            NSString *yearKey = [NSString stringWithFormat:@"year%d", dyear];
            NSMutableDictionary *yearDict = [self.timelineDataDict objectForKey:yearKey];
            if (yearDict != nil) {
                
                NSString *monthKey = [NSString stringWithFormat:@"month%d", dmonth];
                if (dmonth<10) {
                    monthKey=[NSString stringWithFormat:@"month0%d", dmonth];
                }
                NSMutableDictionary *monthDict = [yearDict objectForKey:monthKey];
                if (monthDict != nil) {
                    
                        [unitView setUserInteractionEnabled:YES];
                        [unitView setMultipleTouchEnabled:YES];
                        UIButton *button=[[UIButton alloc]initWithFrame:CGRectMake(unitView.frame.origin.x-unitView.frame.size.width/4, unitView.frame.origin.y, unitView.frame.size.width/2, unitView.frame.size.height)];
                      
                        button.tag=[[[[monthDict objectForKey:@"diarys"] objectAtIndex:0] objectForKey:@"id"] intValue];
                        button.userInteractionEnabled=YES;
                        [button addTarget:self action:@selector(toDiaryView:) forControlEvents:UIControlEventTouchUpInside];
                        [button setBackgroundColor:[UIColor clearColor]];
                    
                        UIImageView *poitImageView = [[UIImageView alloc] initWithFrame:CGRectMake(unitViewWidth/2-12, height/2-10, 6, 6)];
                        [poitImageView setImage:[UIImage imageNamed:@"point_3.png"]];
                        //[button addSubview:poitImageView];
                       [unitView addSubview:poitImageView];
                        [poitImageView release];
                                             
                        [button setCenter:CGPointMake((unitViewWidth/2)*i*scrolValue, (100 -height) /2)];
                        [myScrollView addSubview:button];
                        [scrollSubViews setObject:button forKey:[NSString stringWithFormat:@"button%d",i]];
                        [button release];

                    
                }
                
                
            }
                       
            NSString *unitViewXPosition = [NSString stringWithFormat:@"unit%d", i];
            [scrollSubViews setObject:unitView forKey:unitViewXPosition];

       }
        
        
        ////////////////
        
        
        if(unitCategory==@"day")
        {
            
            if (i %5 == 0) {
                height = 45;
                
            }
            else{
                height = 30;
            }
            UnitView *unitView = [[UnitView alloc] initWithFrame:CGRectMake(0, 0, unitViewWidth*scrolValue, height)];
            [unitView setCenter:CGPointMake((unitViewWidth/2)*i*scrolValue, (100 -height) /2)];
            [scrollBaseView addSubview:unitView];
            
            dday=30- i%30;
            if (dday==0) {
                dday=30;
            }
          
                
            dmonth=abs(curMonth-i/30)%12;
            if (dmonth==0) {
                dmonth=12;
            }
            if (curMonth-i/30<0) {
                dmonth=12-abs(curMonth-i/30)%12;
            }
             
            dyear=curYear-i/360;
           
            if (i%5==0) {
                
                UILabel *danweiLabel=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 80, 20)];
                  [danweiLabel setTextColor:[UIColor lightGrayColor]];
                [danweiLabel setTextAlignment:UITextAlignmentCenter];
                [danweiLabel setBackgroundColor:[UIColor clearColor]];
                [danweiLabel.text sizeWithFont:[UIFont systemFontOfSize:1]];
                danweiLabel.text=[NSString stringWithFormat:@"%d月%d日",dmonth,dday];
              
                [unitView addSubview:danweiLabel];
               
                [danweiLabel release];
            }

            
            NSString *yearKey = [NSString stringWithFormat:@"year%d", dyear];
            NSMutableDictionary *yearDict = [self.timelineDataDict objectForKey:yearKey];
            
            if (yearDict != nil) {
                
                NSString *monthKey = [NSString stringWithFormat:@"month%d", dmonth];
                if (dmonth<10) {
                    monthKey=[NSString stringWithFormat:@"month0%d", dmonth];
                }
                NSMutableDictionary *monthDict = [yearDict objectForKey:monthKey];
                if (monthDict != nil) {
                    
                    NSString *dayKey = [NSString stringWithFormat:@"day%d", dday];
                    
                    if (dday<10) {
                        
                        dayKey=[NSString stringWithFormat:@"day0%d", dday];
                    }
                    NSMutableDictionary *dayDict = [monthDict objectForKey:dayKey];
                    if (dayDict != nil) {
                                                 
                            [unitView setUserInteractionEnabled:YES];
                            [unitView setMultipleTouchEnabled:YES];
                            UIButton *button=[[UIButton alloc]initWithFrame:CGRectMake(unitView.frame.origin.x-unitView.frame.size.width/4, unitView.frame.origin.y, unitView.frame.size.width/2, unitView.frame.size.height)];
                        
                            button.tag=[[[[dayDict objectForKey:@"diarys"] objectAtIndex:0] objectForKey:@"id"] intValue];
                            button.userInteractionEnabled=YES;
                            [button addTarget:self action:@selector(toDiaryView:) forControlEvents:UIControlEventTouchUpInside];
                            [button setBackgroundColor:[UIColor clearColor]];
                            UIImageView *poitImageView = [[UIImageView alloc] initWithFrame:CGRectMake(unitViewWidth/2-12, height/2-10, 6, 6)];
                            [poitImageView setImage:[UIImage imageNamed:@"point_3.png"]];
                           // [button addSubview:poitImageView];
                          [unitView addSubview:poitImageView];
                            [poitImageView release];

                            [button setCenter:CGPointMake((unitViewWidth/2)*i*scrolValue, (100 -height) /2)];
                            [myScrollView addSubview:button];
                            [scrollSubViews setObject:button forKey:[NSString stringWithFormat:@"button%d",i]];
                            [button release];
                        
                    }
                    
                    
                }
                
                
            }
                        
            NSString *unitViewXPosition = [NSString stringWithFormat:@"unit%d", i];
            
            [scrollSubViews setObject:unitView forKey:unitViewXPosition];

            
        }
        
            
        if(unitCategory==@"hour")
            
        {
            
            if (i %3 == 0) {
                height = 45;
               
                
            }
            else{
                height = 30;
            }
            UnitView *unitView = [[UnitView alloc] initWithFrame:CGRectMake(0, 0, unitViewWidth*scrolValue, height)];
            
                       
            [unitView setCenter:CGPointMake((unitViewWidth/2)*i*scrolValue, (100 -height) /2)];
            [scrollBaseView addSubview:unitView];
            
            dhour=24- i%24;
            if (dhour==24) {
                dhour=0;
            }
            if (i<=0) {
                dhour=abs(i)%24;
            }
            
            dday=abs(self.curDay- (i-1)/24)%30;
            if (dday==0) {
                dday=30;
            }
            
            if (self.curDay- i/24<0) {
                dday=30-abs(self.curDay- i/24)%30;
            }
                
            
            if (i%720==0) {
                
                dmonth=abs(curMonth-i/720)%12;
                if (dmonth==0) {
                    dmonth=12;
                }
                if (curMonth-i/720<0) {
                    dmonth=12-abs(curMonth-i/720)%12;
                }
            }
            
            if (i%8640==0) {
                
                dyear=curYear-i/8640;
            }
            

            
            if (i%6==0) {
                UILabel *danweiLabel=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 80, 20)];
                  [danweiLabel setTextColor:[UIColor lightGrayColor]];
                [danweiLabel setTextAlignment:UITextAlignmentCenter];
                [danweiLabel setBackgroundColor:[UIColor clearColor]];
                [danweiLabel.text sizeWithFont:[UIFont systemFontOfSize:1]];
                danweiLabel.text=[NSString stringWithFormat:@"%d日%d时",dday,dhour];
                [unitView addSubview:danweiLabel];
                [danweiLabel release];
            }
 
            
            NSString *yearKey = [NSString stringWithFormat:@"year%d", dyear];
            NSMutableDictionary *yearDict = [self.timelineDataDict objectForKey:yearKey];
            if (yearDict != nil) {
                
                NSString *monthKey = [NSString stringWithFormat:@"month%d", dmonth];
                if (dmonth<10) {
                    monthKey=[NSString stringWithFormat:@"month0%d", dmonth];
                }
                NSMutableDictionary *monthDict = [yearDict objectForKey:monthKey];
                if (monthDict != nil) {
                    
                    NSString *dayKey = [NSString stringWithFormat:@"day%d", dday];
                    if (dday<10) {
                        dayKey=[NSString stringWithFormat:@"day0%d", dday];
                    }
                    NSMutableDictionary *dayDict = [monthDict objectForKey:dayKey];
                    if (dayDict != nil) {
                        NSString *hourKey;
                        if (dhour<10) {
                            hourKey = [NSString stringWithFormat:@"hour0%d", dhour];
                        }
                        else{
                            hourKey = [NSString stringWithFormat:@"hour%d", dhour];
                        }
                    
                        NSMutableDictionary *hourDict = [dayDict objectForKey:hourKey];
                        if (hourDict != nil) {
                           
                            [unitView setUserInteractionEnabled:YES];
                            [unitView setMultipleTouchEnabled:YES];
                            UIButton *button=[[UIButton alloc]initWithFrame:CGRectMake(unitView.frame.origin.x-unitView.frame.size.width/4, unitView.frame.origin.y, unitView.frame.size.width/2, unitView.frame.size.height)];
                            button.tag=[[[[hourDict objectForKey:@"diarys"] objectAtIndex:0] objectForKey:@"id"] intValue];
                            button.userInteractionEnabled=YES;
                            [button addTarget:self action:@selector(toDiaryView:) forControlEvents:UIControlEventTouchUpInside];
                            [button setBackgroundColor:[UIColor clearColor]];
                            UIImageView *poitImageView = [[UIImageView alloc] initWithFrame:CGRectMake(unitViewWidth/2-12, height/2-10, 6, 6)];
                            [poitImageView setImage:[UIImage imageNamed:@"point_3.png"]];
                          //  [button addSubview:poitImageView];
                            [unitView addSubview:poitImageView];
                            [poitImageView release];
                            [button setCenter:CGPointMake((unitViewWidth/2)*i*scrolValue, (100 -height) /2)];
                            [myScrollView addSubview:button];
                            [scrollSubViews setObject:button forKey:[NSString stringWithFormat:@"button%d",i]];
                            [button release];

                            
                        }
                        
                        
                    }
                    
                    
                }
                
                
            }
            NSString *unitViewXPosition = [NSString stringWithFormat:@"unit%d", i];
            
            [scrollSubViews setObject:unitView forKey:unitViewXPosition];

        }
        
        if(unitCategory==@"min"){
            
            
            if (i %5 == 0) {
                height = 45;
                
            }
            else{
                height = 30;
            }
            UnitView *unitView = [[UnitView alloc] initWithFrame:CGRectMake(0, 0, unitViewWidth*scrolValue, height)];
            [unitView setCenter:CGPointMake((unitViewWidth/2)*i*scrolValue, (100 -height) /2)];
            [scrollBaseView addSubview:unitView];
            
            dminute=60-i%60;
            if (dminute<0) {
                dminute=0;
            }
            if (i%60==0&&i!=0) {
                
                dhour=23;
                dminute=0;
            }
            
            if (i%10==0) {
                UILabel *danweiLabel=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 80, 20)];
                  [danweiLabel setTextColor:[UIColor lightGrayColor]];
                [danweiLabel setTextAlignment:UITextAlignmentLeft];
                [danweiLabel setBackgroundColor:[UIColor clearColor]];
                [danweiLabel.text sizeWithFont:[UIFont systemFontOfSize:4]];
                danweiLabel.text=[NSString stringWithFormat:@"%d:%d",dhour,dminute];
                [unitView addSubview:danweiLabel];
                [danweiLabel release];
                
                }
 
            NSString *yearKey = [NSString stringWithFormat:@"year%d", dyear];
            NSMutableDictionary *yearDict = [self.timelineDataDict objectForKey:yearKey];
            if (yearDict != nil) {
                
                NSString *monthKey = [NSString stringWithFormat:@"month%d", dmonth];
                if (dmonth<10) {
                    monthKey=[NSString stringWithFormat:@"month0%d", dmonth];
                }
                NSMutableDictionary *monthDict = [yearDict objectForKey:monthKey];
                if (monthDict != nil) {
                    
                    NSString *dayKey = [NSString stringWithFormat:@"day%d", dday];
                    if (dday<10) {
                        dayKey=[NSString stringWithFormat:@"day0%d", dday];
                    }
                    NSMutableDictionary *dayDict = [monthDict objectForKey:dayKey];
                    if (dayDict != nil) {
                        
                        NSString *hourKey = [NSString stringWithFormat:@"hour0%d", dhour];
                        
                        NSMutableDictionary *hourDict = [dayDict objectForKey:hourKey];
                        if (hourDict != nil) {
                            
                            NSString *minKey = [NSString stringWithFormat:@"min%d", dminute];
                            if (lineCount-i-1<10) {
                                minKey=[NSString stringWithFormat:@"min0%d", dminute];
                            }
                            NSMutableDictionary *minDict = [hourDict objectForKey:minKey];
                            if (minDict != nil) {
                                if([[minDict objectForKey:@"diarys"] count]!=0)   {
                                    
                                    [unitView setUserInteractionEnabled:YES];
                                    [unitView setMultipleTouchEnabled:YES];
                                    
                                    UIButton *button=[[UIButton alloc]initWithFrame:CGRectMake(unitView.frame.origin.x-unitView.frame.size.width/4, unitView.frame.origin.y, unitView.frame.size.width/2, unitView.frame.size.height)];
                                    
                                    button.tag=[[[[minDict objectForKey:@"diarys"] objectAtIndex:0] objectForKey:@"id"] intValue];
                                   
                                    button.userInteractionEnabled=YES;
                                    [button addTarget:self action:@selector(toDiaryView:) forControlEvents:UIControlEventTouchUpInside];
                                    [button setBackgroundColor:[UIColor clearColor]];
                                    UIImageView *poitImageView = [[UIImageView alloc] initWithFrame:CGRectMake (unitViewWidth/2-12, height/2-10, 6, 6)];
                                    [poitImageView setImage:[UIImage imageNamed:@"point_3.png"]];
                                    //[button addSubview:poitImageView];
                                    [unitView addSubview:poitImageView];
                                    [poitImageView release];

                                    [button setCenter:CGPointMake((unitViewWidth/2)*i*scrolValue, (100 -height) /2)];
                                    [myScrollView addSubview:button];
                                    [scrollSubViews setObject:button forKey:[NSString stringWithFormat:@"button%d",i]];
                                    [button release];

                                    
                                }
                                
                            }
                            
                        }
                        
                        
                    }
                    
                    
                }
                
                
            }
            
           
            
            NSString *unitViewXPosition = [NSString stringWithFormat:@"unit%d", i];
            
            [scrollSubViews setObject:unitView forKey:unitViewXPosition];

        }
        
        
}
    
    indexUp = lineCount;
    indexDown = 1;
    
    NSLog(@"Scroll View Count %d", [[scrollBaseView subviews] count]);
        
    [myScrollView setContentOffset:CGPointMake(0, 0) animated:YES];

    
    [myScrollView setContentSize:CGSizeMake(lineCount*unitViewWidth*scrolValue, 40)];
    
    CGAffineTransform transform = CGAffineTransformMakeRotation(-90.0f /180.0f *3.1415926);
    
    myTableView.transform = transform;
        
    }
    [LoadingView hideWithAnimated:YES];
   
}


-(void)rulerScrollToDiaryDate:(long long int)_diaryDate{
    
    //滚动时间轴时刻与日志内容统一
    //获取当时时间的年月日整型数
    NSDate *nd = [NSDate date];
    NSCalendar *cal = [NSCalendar currentCalendar];
    unsigned int unitFlags =NSYearCalendarUnit| NSMonthCalendarUnit| NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit;
    NSDateComponents *dd = [cal components:unitFlags fromDate:nd];
    int cur_year = [dd year];
    int cur_month =[dd month];
    int cur_day =[dd day];
    int cur_hour  = [dd hour];
    int cur_min = [dd minute];
    
//    NSDate *diaryDate = [NSDate dateWithTimeIntervalSince1970:_diaryDate/1000];
//    NSDateComponents *diarydd = [cal components:unitFlags fromDate:diaryDate];
//    int diary_year = [diarydd year];
//    int diary_month = [diarydd month];
//    int diary_day = [diarydd day];
//    int diary_hour = [diarydd hour];
    
    
    float width ;
    
    NSTimeInterval time = [[NSDate date] timeIntervalSince1970];
    long long int curtime = (long long int)time; 
    long long int diarytime = _diaryDate;
    diarytime = diarytime/1000;
    
    NSString *cur_yearStr = [NSString stringWithFormat:@"%d",cur_year];
    NSString *cur_monthStr = [NSString stringWithFormat:@"%d",cur_month];
    NSString *cur_dayStr = [NSString stringWithFormat:@"%d",cur_day];
    NSString *cur_hourStr = [NSString stringWithFormat:@"%d",cur_hour];
    NSString *cur_minStr = [NSString stringWithFormat:@"%d",cur_min];
    
    if (cur_month<10) {
        cur_monthStr = [NSString stringWithFormat:@"0%d",cur_month];
    }
    if (cur_day < 10) {
        cur_dayStr = [NSString stringWithFormat:@"0%d",cur_day];
    }
    if (cur_hour <10) {
        cur_dayStr = [NSString stringWithFormat:@"0%d",cur_hour];
    }
    if (cur_min < 10) {
        cur_minStr = [NSString stringWithFormat:@"0%d",cur_min];
    }
    
    
    if (unitCategory == @"min") {
        
        NSDate *beginDate =  [self dateFromString:[NSString stringWithFormat:@"%@-%@-%@ %@:%@:%@",cur_yearStr,cur_monthStr,cur_dayStr,@"23",@"59",@"59"]];

        NSTimeInterval t = [beginDate timeIntervalSince1970];
        long long  int rulerBeginTime = (long long int)t;
        
        //width = ((curtime- diarytime)/60 + (24-cur_hour)*60)*(unitViewWidth/2)*scrolValue-160;
        
        width = (rulerBeginTime/60 - diarytime/60)*(unitViewWidth/2)*scrolValue-160;
        
    }
    if (unitCategory == @"hour") {
        
        NSDate *beginDate =  [self dateFromString:[NSString stringWithFormat:@"%@-%@-%@ %@:%@:%@",cur_yearStr,cur_monthStr,cur_dayStr,@"23",@"59",@"59"]];
        
        NSTimeInterval t = [beginDate timeIntervalSince1970];
        long long  int rulerBeginTime = (long long int)t;
        
        //width = ((curtime- diarytime)/60 + (24-cur_hour)*60)*(unitViewWidth/2)*scrolValue-160;
        
        width =( (rulerBeginTime/60 - diarytime/60)/60)*(unitViewWidth/2)*scrolValue-160;

        CLog(@"%@", [NSString stringWithFormat:@"%@-%@-%@ %@:%@:%@",cur_yearStr,cur_monthStr,cur_dayStr,@"23",@"59",@"59"]);
        
       // width = ((curtime-diarytime)/3600 + 24-cur_hour)*(unitViewWidth/2)*scrolValue-160;
        
    }
    if (unitCategory == @"day") {
        
        NSDate *beginDate =  [self dateFromString:[NSString stringWithFormat:@"%@-%@-%@ %@:%@:%@",cur_yearStr,cur_monthStr,@"30",@"23",@"59",@"59"]];
        
        NSTimeInterval t = [beginDate timeIntervalSince1970];
        long long  int rulerBeginTime = (long long int)t;
        
        //width = ((curtime- diarytime)/60 + (24-cur_hour)*60)*(unitViewWidth/2)*scrolValue-160;
        
        width =(( (rulerBeginTime/60 - diarytime/60)/60)/24)*(unitViewWidth/2)*scrolValue-160;
        

        
       // width = (curtime/60/60/24 - diarytime/60/60/24 +30-cur_day)*(unitViewWidth/2)*scrolValue-160;
        
    }
    if (unitCategory == @"month") {
        
        NSDate *beginDate =  [self dateFromString:[NSString stringWithFormat:@"%@-%@-%@ %@:%@:%@",cur_yearStr,@"12",@"30",@"23",@"59",@"59"]];
        
        NSTimeInterval t = [beginDate timeIntervalSince1970];
        long long  int rulerBeginTime = (long long int)t;
        
        //width = ((curtime- diarytime)/60 + (24-cur_hour)*60)*(unitViewWidth/2)*scrolValue-160;
        
        width =((( (rulerBeginTime/60 - diarytime/60)/60)/24)/30)*(unitViewWidth/2)*scrolValue-160;

        
        
      //  width = (curtime/60/60/24/30 - diarytime/60/60/24/30 )*(unitViewWidth/2)*scrolValue-160;
        
    }
    
    
    [self.myScrollView setContentOffset:CGPointMake(width, 0) animated:NO];
}

- (NSDate *)dateFromString:(NSString *)dateString{
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    
    [dateFormatter setDateFormat: @"yyyy-MM-dd HH:mm:ss"]; 
    
    
    NSDate *destDate= [dateFormatter dateFromString:dateString];
    
    
    
    [dateFormatter release];
    
    return destDate;
    
}


#pragma mark scrollview delegate

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    
   // NSLog(@"Scroll View did scroll!");
    
    CGPoint point = scrollView.contentOffset;
    
    float pointInt = point.x /((unitViewWidth/2)*scrolValue);
    float pointX = point.x;
    float pointY = point.y;
    
    NSInteger newIndexUP = pointInt +lineCount;
    NSInteger newIndexDown = pointInt -lineCount/2;
    
    float height = 0;
    
    //如果与当前显示没有交集
    if (newIndexDown>indexUp || newIndexUP < indexDown) {
        
        for (int i = newIndexDown; i <= newIndexUP; i++) {
            
            if(unitCategory==@"min"){
                
                if (i %5 == 0) {
                    height = 45;
                }
                else{
                    height = 30;
                }
                UnitView *unitView = [[UnitView alloc] initWithFrame:CGRectMake(0, 0, unitViewWidth*scrolValue, height)];
                
                unitView.tag=10;
                [unitView setCenter:CGPointMake(10*i*scrolValue, (100 -height) /2)];
                [scrollBaseView addSubview:unitView];
                
                dminute=60-i%60;
                if (dminute==60) {
                    dminute=0;
                }
                
                dhour=23-((i-1)/60)%24;
                
                dday=abs(self.curDay-i/1440)%30;
                if (dday==0) {
                    dday=30;
                }
                
                if (self.curDay-i/1440<0) {
                    dday=30-abs(self.curDay-i/1440)%30;
                }
                
                if((currentTime - i*60) < rulerBeginFlag/1000)
                {                    //刷新数据 
                    
                    begindate=begindate-86400000*7; 
                    
                    enddate = enddate-86400000*7; 
                    
                    rulerBeginFlag = begindate;
                    
                    NSString *enddateStr = [NSString stringWithFormat:@"%lld", enddate];
                    NSString *begindateStr = [NSString stringWithFormat:@"%lld", begindate];
                    
                    [self loadRulerDataWith:begindateStr endtime:enddateStr];
                }
                
                
                dmonth=abs(curMonth-i/43200)%12;
                
                if (dmonth==0) {
                    dmonth=12;
                }
                if (curMonth-i/43200<0) {
                    dmonth=12-abs(curMonth-i/43200)%12;
                }
                dyear=curYear-i/518400;
                
                
                if (i%10==0) {
                    UILabel *danweiLabel=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 80, 20)];
                      [danweiLabel setTextColor:[UIColor lightGrayColor]];
                    [danweiLabel setTextAlignment:UITextAlignmentLeft];
                    [danweiLabel setBackgroundColor:[UIColor clearColor]];
                    [danweiLabel.text sizeWithFont:[UIFont systemFontOfSize:4]];
                    
                    danweiLabel.text=[NSString stringWithFormat:@"%d:%d",dhour,dminute];
                    [unitView addSubview:danweiLabel];
                    [danweiLabel release];
                }
                
                
                NSString *yearKey = [NSString stringWithFormat:@"year%d", dyear];
                NSMutableDictionary *yearDict = [self.timelineDataDict objectForKey:yearKey];
                if (yearDict != nil) {
                    
                    NSString *monthKey = [NSString stringWithFormat:@"month%d", dmonth];
                    if (dmonth<10) {
                        monthKey=[NSString stringWithFormat:@"month0%d", dmonth];
                    }
                    NSMutableDictionary *monthDict = [yearDict objectForKey:monthKey];
                    if (monthDict != nil) {
                        
                        NSString *dayKey = [NSString stringWithFormat:@"day%d", dday];
                        if (dday<10) {
                            dayKey=[NSString stringWithFormat:@"day0%d", dday];
                        }
                        NSMutableDictionary *dayDict = [monthDict objectForKey:dayKey];
                        if (dayDict != nil) {
                            
                            NSString *hourKey = [NSString stringWithFormat:@"hour%d", dhour];
                            if (dhour<10) {
                                hourKey = [NSString stringWithFormat:@"hour0%d", dhour];
                                
                            }
                            
                            NSMutableDictionary *hourDict = [dayDict objectForKey:hourKey];
                            if (hourDict != nil) {
                                
                                NSString *minKey = [NSString stringWithFormat:@"min%d", dminute];
                                if (i<10) {
                                    minKey=[NSString stringWithFormat:@"min0%d", dminute];
                                }
                                NSMutableDictionary *minDict = [hourDict objectForKey:minKey];
                                if (minDict != nil) {
                                    if([[minDict objectForKey:@"diarys"] count]!=0)   {
                                        
                                        // unitView.backgroundColor=[UIColor redColor];
                                        [unitView setUserInteractionEnabled:YES];
                                        [unitView setMultipleTouchEnabled:YES];
                                        //UIButton *button=[[UIButton alloc]initWithFrame:CGRectMake(0, 0, 50, 40)];
                                        
                                        UIButton *button=[[UIButton alloc]initWithFrame:CGRectMake(unitView.frame.origin.x-unitView.frame.size.width/4, unitView.frame.origin.y, unitView.frame.size.width/2, unitView.frame.size.height)];
                                        
                                        button.tag=[[[[minDict objectForKey:@"diarys"] objectAtIndex:0] objectForKey:@"id"] intValue];
                                        
                                        
                                        button.userInteractionEnabled=YES;
                                        [button addTarget:self action:@selector(toDiaryView:) forControlEvents:UIControlEventTouchUpInside];
                                        [button setBackgroundColor:[UIColor clearColor]];
                                        UIImageView *poitImageView = [[UIImageView alloc] initWithFrame:CGRectMake (unitViewWidth/2-12, unitView.frame.size.height/2-10, 6, 6)];
                                        [poitImageView setImage:[UIImage imageNamed:@"point_3.png"]];
                                        [unitView addSubview:poitImageView];
                                       // [button addSubview:poitImageView];
                                        [poitImageView release];
                                        
                                        [button setCenter:CGPointMake((unitViewWidth/2)*i*scrolValue, (100 -height) /2)];
                                        [myScrollView addSubview:button];
                                        [scrollSubViews setObject:button forKey:[NSString stringWithFormat:@"button%d",i]];
                                        [button release];
                                        
                                        
                                    }
                                    
                                }
                                
                            }
                            
                            
                        }
                        
                        
                    }
                    
                    
                }
                NSString *unitViewXPosition = [NSString stringWithFormat:@"unit%d", i];
                
                [scrollSubViews setObject:unitView forKey:unitViewXPosition];
            }//category
            
            if(unitCategory==@"hour")
            {
                
                if (i %3 == 0) {
                    height = 45;
                }
                else{
                    height = 30;
                }
                UnitView *unitView = [[UnitView alloc] initWithFrame:CGRectMake(0, 0, unitViewWidth*scrolValue, height)];
                [unitView setCenter:CGPointMake(10*i*scrolValue, (100 -height) /2)];
                [scrollBaseView addSubview:unitView];
                
                
                dhour=24-i%24;
                if (dhour==24) {
                    dhour=0;
                }
                
                dday=abs(self.curDay-(i-1)/24)%30;
                if (dday==0) {
                    dday=30;
                }
                if (self.curDay-i/24<0) {
                    dday=30-abs(self.curDay-(i-1)/24)%30;
                }
                
                dmonth=abs(curMonth-i/720)%12;
                if (dmonth==0) {
                    dmonth=12;
                    
                }
                if (curMonth-i/720<0) {
                    dmonth=12-abs(curMonth-i/720)%12;
                }
                
                if((currentTime - i*60*60) < rulerBeginFlag/1000){  //a week later
                    //刷新数据 
                    
                    begindate=begindate-86400000*7; 
                    
                    enddate = enddate-86400000*7; 
                    
                    rulerBeginFlag = begindate;
                    
                    NSString *enddateStr = [NSString stringWithFormat:@"%lld", enddate];
                    NSString *begindateStr = [NSString stringWithFormat:@"%lld", begindate];
                    
                    [self loadRulerDataWith:begindateStr endtime:enddateStr];
                }
                
                
                dyear=curYear-i/8640;
                
                
                if (i%6==0) {
                    UILabel *danweiLabel=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 80, 20)];
                      [danweiLabel setTextColor:[UIColor lightGrayColor]];
                    [danweiLabel setTextAlignment:UITextAlignmentCenter];
                    [danweiLabel setBackgroundColor:[UIColor clearColor]];
                    [danweiLabel.text sizeWithFont:[UIFont systemFontOfSize:3]];
                    
                    danweiLabel.text=[NSString stringWithFormat:@"%d日%d时",dday,dhour];
                    [unitView addSubview:danweiLabel];
                    
                    [danweiLabel release];
                }
                
                
                NSString *yearKey = [NSString stringWithFormat:@"year%d", dyear];
                NSMutableDictionary *yearDict = [self.timelineDataDict objectForKey:yearKey];
                if (yearDict != nil) {
                    
                    NSString *monthKey = [NSString stringWithFormat:@"month%d", dmonth];
                    if (dmonth<10) {
                        monthKey=[NSString stringWithFormat:@"month0%d", dmonth];
                    }
                    NSMutableDictionary *monthDict = [yearDict objectForKey:monthKey];
                    if (monthDict != nil) {
                        
                        NSString *dayKey = [NSString stringWithFormat:@"day%d", dday];
                        if (dday<10) {
                            dayKey=[NSString stringWithFormat:@"day0%d", dday];
                        }
                        NSMutableDictionary *dayDict = [monthDict objectForKey:dayKey];
                        if (dayDict != nil) {
                            
                            NSString *hourKey = [NSString stringWithFormat:@"hour%d", dhour];
                            if (dhour<10) {
                                hourKey = [NSString stringWithFormat:@"hour0%d", dhour];
                                
                            }
                            
                            NSMutableDictionary *hourDict = [dayDict objectForKey:hourKey];
                            if (hourDict != nil) {
                                
                                
                                if([[hourDict objectForKey:@"diarys"] count]!=0)   {
                                    
                                    //  unitView.backgroundColor=[UIColor redColor];
                                    [unitView setUserInteractionEnabled:YES];
                                    [unitView setMultipleTouchEnabled:YES];
                                    //UIButton *button=[[UIButton alloc]initWithFrame:CGRectMake(0, 0, 50, 40)];
                                    
                                    UIButton *button=[[UIButton alloc]initWithFrame:CGRectMake(unitView.frame.origin.x-unitView.frame.size.width/4, unitView.frame.origin.y, unitView.frame.size.width/2, unitView.frame.size.height)];
                                    
                                    button.tag=[[[[hourDict objectForKey:@"diarys"] objectAtIndex:0] objectForKey:@"id"] intValue];
                                    
                                    
                                    button.userInteractionEnabled=YES;
                                    [button addTarget:self action:@selector(toDiaryView:) forControlEvents:UIControlEventTouchUpInside];
                                    [button setBackgroundColor:[UIColor clearColor]];
                                    UIImageView *poitImageView = [[UIImageView alloc] initWithFrame:CGRectMake (unitViewWidth/2-12, height/2-10, 6, 6)];
                                    [poitImageView setImage:[UIImage imageNamed:@"point_3.png"]];
                                    //[button addSubview:poitImageView];
                                    [unitView addSubview:poitImageView];
                                    [poitImageView release];
                                    
                                    
                                    [button setCenter:CGPointMake((unitViewWidth/2)*i*scrolValue, (100 -height) /2)];
                                    [myScrollView addSubview:button];
                                    [scrollSubViews setObject:button forKey:[NSString stringWithFormat:@"button%d",i]];
                                    [button release];
                                    
                                    
                                }
                                
                            }
                            
                            
                        }
                        
                        
                    }
                    
                    
                }
                NSString *unitViewXPosition = [NSString stringWithFormat:@"unit%d", i];
                
                [scrollSubViews setObject:unitView forKey:unitViewXPosition];
            }//category
            
            
            if(unitCategory==@"day")
            {
                
                if (i %5 == 0) {
                    height = 45;
                }
                else{
                    height = 30;
                }
                UnitView *unitView = [[UnitView alloc] initWithFrame:CGRectMake(0, 0, unitViewWidth*scrolValue, height)];
                [unitView setCenter:CGPointMake(10*i*scrolValue, (100 -height) /2)];
                [scrollBaseView addSubview:unitView];
                
                
                dday=30-i%30;
                if (dday==0) {
                    dday=30;
                }
                
                dmonth=abs(curMonth-i/30)%12;
                if (dmonth==0) {
                    dmonth=12;
                }
                if (curMonth-i/30<0) {
                    dmonth=12-abs(curMonth-i/30)%12;
                }
                
                dyear=curYear-i/360;
               if((currentTime - i*60*60*24) < rulerBeginFlag/1000) {  //a month later
                    //刷新数据 
                    
                    //begindate=begindate-86400000*30; 
                    begindate=begindate-86400000*10;
                    begindate=begindate-86400000*10;
                    begindate=begindate-86400000*10;
                    
                    //enddate = enddate-86400000*30; 
                    enddate = enddate-86400000*10; 
                    enddate = enddate-86400000*10; 
                    enddate = enddate-86400000*10; 
                   
                   rulerBeginFlag = begindate;
                    
                    NSString *enddateStr = [NSString stringWithFormat:@"%lld", enddate];
                    NSString *begindateStr = [NSString stringWithFormat:@"%lld", begindate];
                    
                    [self loadRulerDataWith:begindateStr endtime:enddateStr];
                }
                
                
                if (i%5==0) {
                    UILabel *danweiLabel=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 80, 20)];
                      [danweiLabel setTextColor:[UIColor lightGrayColor]];
                    [danweiLabel setTextAlignment:UITextAlignmentCenter];
                    [danweiLabel setBackgroundColor:[UIColor clearColor]];
                    [danweiLabel.text sizeWithFont:[UIFont systemFontOfSize:1]];
                    
                    danweiLabel.text=[NSString stringWithFormat:@"%d月%d日",dmonth,dday];
                    
                    [unitView addSubview:danweiLabel];
                    
                    [danweiLabel release];
                }
                
                NSString *yearKey = [NSString stringWithFormat:@"year%d", dyear];
                NSMutableDictionary *yearDict = [self.timelineDataDict objectForKey:yearKey];
                if (yearDict != nil) {
                    
                    NSString *monthKey = [NSString stringWithFormat:@"month%d", dmonth];
                    if (dmonth<10) {
                        monthKey=[NSString stringWithFormat:@"month0%d", dmonth];
                    }
                    NSMutableDictionary *monthDict = [yearDict objectForKey:monthKey];
                    if (monthDict != nil) {
                        
                        NSString *dayKey = [NSString stringWithFormat:@"day%d", dday];
                        if (dday<10) {
                            dayKey=[NSString stringWithFormat:@"day0%d", dday];
                        }
                        NSMutableDictionary *dayDict = [monthDict objectForKey:dayKey];
                        if (dayDict != nil) {
                            
                            
                            if([[dayDict objectForKey:@"diarys"] count]!=0)   {
                                
                                //unitView.backgroundColor=[UIColor redColor];
                                [unitView setUserInteractionEnabled:YES];
                                [unitView setMultipleTouchEnabled:YES];
                                //UIButton *button=[[UIButton alloc]initWithFrame:CGRectMake(0, 0, 50, 40)];
                                
                                UIButton *button=[[UIButton alloc]initWithFrame:CGRectMake(unitView.frame.origin.x-unitView.frame.size.width/4, unitView.frame.origin.y, unitView.frame.size.width/2, unitView.frame.size.height)];
                                
                                button.tag=[[[[dayDict objectForKey:@"diarys"] objectAtIndex:0] objectForKey:@"id"] intValue];
                                
                                
                                button.userInteractionEnabled=YES;
                                [button addTarget:self action:@selector(toDiaryView:) forControlEvents:UIControlEventTouchUpInside];
                                [button setBackgroundColor:[UIColor clearColor]];
                                UIImageView *poitImageView = [[UIImageView alloc] initWithFrame:CGRectMake (unitViewWidth/2-12, height/2-10, 6, 6)];
                                [poitImageView setImage:[UIImage imageNamed:@"point_3.png"]];
                               // [button addSubview:poitImageView];
                                [unitView addSubview:poitImageView];
                                [poitImageView release];
                                
                                [button setCenter:CGPointMake((unitViewWidth)*i*scrolValue, (100 -height) /2)];
                                [myScrollView addSubview:button];
                                [scrollSubViews setObject:button forKey:[NSString stringWithFormat:@"button%d",i]];
                                [button release];
                                
                                
                            }
                            
                        }
                        
                        
                    }
                    
                    
                }
                NSString *unitViewXPosition = [NSString stringWithFormat:@"unit%d", i];
                
                [scrollSubViews setObject:unitView forKey:unitViewXPosition];
            }//category
            
            if(unitCategory==@"month")
            {
                if (i %3 == 0) {
                    height = 45;
                }
                else{
                    height = 30;
                }
                UnitView *unitView = [[UnitView alloc] initWithFrame:CGRectMake(0, 0, unitViewWidth*scrolValue, height)];
                [unitView setCenter:CGPointMake(10*i*scrolValue, (100 -height) /2)];
                [scrollBaseView addSubview:unitView];
                
                dmonth=12-i%12;
                if (dmonth==0) {
                    dmonth=12;
                }
                if (i<0) {
                    dmonth=12-abs(i)%12;
                }
                dyear=curYear-i/12;
                
                 if((currentTime - i*60*60*24*30) < rulerBeginFlag/1000){    // 30 month later
                    //刷新数据
                    
                    begindate=begindate/1000-86400*30*30; 
                    begindate=begindate*1000;
                    
                    //  enddate = enddate-86400000*30*30; 
                    enddate = enddate/1000-86400*30*30; 
                    enddate = enddate*1000;
                     
                     rulerBeginFlag = begindate;
                    
                    NSString *enddateStr = [NSString stringWithFormat:@"%lld", enddate];
                    NSString *begindateStr = [NSString stringWithFormat:@"%lld", begindate];
                    
                    [self loadRulerDataWith:begindateStr endtime:enddateStr];
                    
                }
                
                
                if (i%3==0) {
                    UILabel *danweiLabel=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 20)];
                      [danweiLabel setTextColor:[UIColor lightGrayColor]];
                    [danweiLabel setTextAlignment:UITextAlignmentLeft];
                    [danweiLabel setBackgroundColor:[UIColor clearColor]];
                    [danweiLabel.text sizeWithFont:[UIFont systemFontOfSize:1]];
                    
                    danweiLabel.text=[NSString stringWithFormat:@"%d年%d月",dyear,dmonth];
                    // [danweiLabel setCenter:CGPointMake(10*i*scrolValue, 10)];
                    [unitView addSubview:danweiLabel];
                    
                    [danweiLabel release];
                }
                
                
                NSString *yearKey = [NSString stringWithFormat:@"year%d", dyear];
                NSMutableDictionary *yearDict = [self.timelineDataDict objectForKey:yearKey];
                if (yearDict != nil) {
                    
                    NSString *monthKey = [NSString stringWithFormat:@"month%d", dmonth];
                    if (dmonth<10) {
                        monthKey=[NSString stringWithFormat:@"month0%d", dmonth];
                    }
                    NSMutableDictionary *monthDict = [yearDict objectForKey:monthKey];
                    if (monthDict != nil) {
                        
                        
                        //unitView.backgroundColor=[UIColor redColor];
                        [unitView setUserInteractionEnabled:YES];
                        [unitView setMultipleTouchEnabled:YES];
                        //UIButton *button=[[UIButton alloc]initWithFrame:CGRectMake(0, 0, 50, 40)];
                        
                        UIButton *button=[[UIButton alloc]initWithFrame:CGRectMake(unitView.frame.origin.x-unitView.frame.size.width/4, unitView.frame.origin.y, unitView.frame.size.width/2, unitView.frame.size.height)];
                        
                        button.tag=[[[[monthDict objectForKey:@"diarys"] objectAtIndex:0] objectForKey:@"id"] intValue];
                        
                        
                        button.userInteractionEnabled=YES;
                        [button addTarget:self action:@selector(toDiaryView:) forControlEvents:UIControlEventTouchUpInside];
                        [button setBackgroundColor:[UIColor clearColor]];
                        UIImageView *poitImageView = [[UIImageView alloc] initWithFrame:CGRectMake(unitViewWidth/2-12, height/2-10, 6, 6)];
                        [poitImageView setImage:[UIImage imageNamed:@"point_3.png"]];
                       // [button addSubview:poitImageView];
                        [unitView addSubview:poitImageView];
                        [poitImageView release];
                        
                        
                        [button setCenter:CGPointMake((unitViewWidth/2)*i*scrolValue, (100 -height) /2)];
                        [myScrollView addSubview:button];
                        
                        [scrollSubViews setObject:button forKey:[NSString stringWithFormat:@"button%d",i]];
                        [button release];
                        
                        
                    }
                    
                    
                }
                NSString *unitViewXPosition = [NSString stringWithFormat:@"unit%d", i];
                
                [scrollSubViews setObject:unitView forKey:unitViewXPosition];
            }
            
            CLog(@"i %d", i);
            CLog(@"dyear%d dmonth%d dday%d dhour%d dmin%d", dyear,dmonth,dday,dhour,dminute);
            
        }
        
        indexDown = newIndexDown;
        indexUp = newIndexUP;
        
    }
    
    
    
    //是否超出当前显示上限
    if (newIndexUP >indexUp && newIndexDown < indexUp) {
        
                    
        for (int i = indexUp +1; i <= newIndexUP; i++) {
                       
            if(unitCategory==@"min"){
                
                if (i %5 == 0) {
                    height = 45;
                }
                else{
                    height = 30;
                }
                UnitView *unitView = [[UnitView alloc] initWithFrame:CGRectMake(0, 0, unitViewWidth*scrolValue, height)];
              
                unitView.tag=10;
                [unitView setCenter:CGPointMake(10*i*scrolValue, (100 -height) /2)];
                [scrollBaseView addSubview:unitView];
                            
                dminute=60-i%60;
                if (dminute==60) {
                    dminute=0;
                }
                
                dhour=23-((i-1)/60)%24;
               
                dday=abs(self.curDay-i/1440)%30;
                if (dday==0) {
                    dday=30;
                }
                
                if (self.curDay-i/1440<0) {
                    dday=30-abs(self.curDay-i/1440)%30;
                }
                   
                     if((currentTime - i*60) < rulerBeginFlag/1000) {  //a week later
                    //刷新数据 
                         
                    begindate=begindate-86400000*7; 
                    
                    enddate = enddate-86400000*7; 
                         
                         rulerBeginFlag = begindate;
                    
                    NSString *enddateStr = [NSString stringWithFormat:@"%lld", enddate];
                    NSString *begindateStr = [NSString stringWithFormat:@"%lld", begindate];
                    
                         [self loadRulerDataWith:begindateStr endtime:enddateStr];
                    }
              
                    
                    dmonth=abs(curMonth-i/43200)%12;
                
                    if (dmonth==0) {
                        dmonth=12;
                    }
                    if (curMonth-i/43200<0) {
                        dmonth=12-abs(curMonth-i/43200)%12;
                    }
                    dyear=curYear-i/518400;
                            
                               
                if (i%10==0) {
                    UILabel *danweiLabel=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 80, 20)];
                      [danweiLabel setTextColor:[UIColor lightGrayColor]];
                    [danweiLabel setTextAlignment:UITextAlignmentLeft];
                    [danweiLabel setBackgroundColor:[UIColor clearColor]];
                    [danweiLabel.text sizeWithFont:[UIFont systemFontOfSize:4]];
                   
                    danweiLabel.text=[NSString stringWithFormat:@"%d:%d",dhour,dminute];
                    [unitView addSubview:danweiLabel];
                    [danweiLabel release];
                }

             
            NSString *yearKey = [NSString stringWithFormat:@"year%d", dyear];
            NSMutableDictionary *yearDict = [self.timelineDataDict objectForKey:yearKey];
            if (yearDict != nil) {
                
                NSString *monthKey = [NSString stringWithFormat:@"month%d", dmonth];
                if (dmonth<10) {
                    monthKey=[NSString stringWithFormat:@"month0%d", dmonth];
                }
                NSMutableDictionary *monthDict = [yearDict objectForKey:monthKey];
                if (monthDict != nil) {
                    
                    NSString *dayKey = [NSString stringWithFormat:@"day%d", dday];
                    if (dday<10) {
                        dayKey=[NSString stringWithFormat:@"day0%d", dday];
                    }
                    NSMutableDictionary *dayDict = [monthDict objectForKey:dayKey];
                    if (dayDict != nil) {
                        
                        NSString *hourKey = [NSString stringWithFormat:@"hour%d", dhour];
                        if (dhour<10) {
                            hourKey = [NSString stringWithFormat:@"hour0%d", dhour];

                        }
                        
                        NSMutableDictionary *hourDict = [dayDict objectForKey:hourKey];
                        if (hourDict != nil) {
                            
                            NSString *minKey = [NSString stringWithFormat:@"min%d", dminute];
                            if (i<10) {
                                minKey=[NSString stringWithFormat:@"min0%d", dminute];
                            }
                            NSMutableDictionary *minDict = [hourDict objectForKey:minKey];
                            if (minDict != nil) {
                                if([[minDict objectForKey:@"diarys"] count]!=0)   {
                                    
                                   // unitView.backgroundColor=[UIColor redColor];
                                    [unitView setUserInteractionEnabled:YES];
                                    [unitView setMultipleTouchEnabled:YES];
                                    //UIButton *button=[[UIButton alloc]initWithFrame:CGRectMake(0, 0, 50, 40)];
                                    
                                    UIButton *button=[[UIButton alloc]initWithFrame:CGRectMake(unitView.frame.origin.x-unitView.frame.size.width/4, unitView.frame.origin.y, unitView.frame.size.width/2, unitView.frame.size.height)];                                    
                                    button.tag=[[[[minDict objectForKey:@"diarys"] objectAtIndex:0] objectForKey:@"id"] intValue];
                                    
                                    
                                    button.userInteractionEnabled=YES;
                                    [button addTarget:self action:@selector(toDiaryView:) forControlEvents:UIControlEventTouchUpInside];
                                    [button setBackgroundColor:[UIColor clearColor]];
                                    UIImageView *poitImageView = [[UIImageView alloc] initWithFrame:CGRectMake (unitViewWidth/2-12, height/2-10, 6, 6)];
                                    [poitImageView setImage:[UIImage imageNamed:@"point_3.png"]];
                                  //  [button addSubview:poitImageView];
                                    [unitView addSubview:poitImageView];
                                    [poitImageView release];

                                    [button setCenter:CGPointMake((unitViewWidth/2)*i*scrolValue, (100 -height) /2)];
                                    [myScrollView addSubview:button];
                                    [scrollSubViews setObject:button forKey:[NSString stringWithFormat:@"button%d",i]];
                                    [button release];

                                    
                                }
                                
                            }
                            
                        }
                        
                        
                    }
                    
                    
                }
                
                
            }
                NSString *unitViewXPosition = [NSString stringWithFormat:@"unit%d", i];
                
                [scrollSubViews setObject:unitView forKey:unitViewXPosition];
        }//category

            if(unitCategory==@"hour")
            {
                
                if (i %3 == 0) {
                    height = 45;
                }
                else{
                    height = 30;
                }
                UnitView *unitView = [[UnitView alloc] initWithFrame:CGRectMake(0, 0, unitViewWidth*scrolValue, height)];
                [unitView setCenter:CGPointMake(10*i*scrolValue, (100 -height) /2)];
                [scrollBaseView addSubview:unitView];
                
                
                dhour=24-i%24;
                if (dhour==24) {
                    dhour=0;
                }
                
                    dday=abs(self.curDay-(i-1)/24)%30;
                    if (dday==0) {
                        dday=30;
                    }
                    if (self.curDay-i/24<0) {
                        dday=30-abs(self.curDay-(i-1)/24)%30;
                    }
                                        
                    dmonth=abs(curMonth-i/720)%12;
                    if (dmonth==0) {
                        dmonth=12;
                        
                    }
                    if (curMonth-i/720<0) {
                        dmonth=12-abs(curMonth-i/720)%12;
                    }
                      
                if((currentTime - i*60*60) < rulerBeginFlag/1000) {  //a week later
                    //刷新数据 
                    
                    begindate=begindate-86400000*7; 
                    
                    enddate = enddate-86400000*7; 
                    
                    rulerBeginFlag = begindate;
                    
                    NSString *enddateStr = [NSString stringWithFormat:@"%lld", enddate];
                    NSString *begindateStr = [NSString stringWithFormat:@"%lld", begindate];
                    
                    [self loadRulerDataWith:begindateStr endtime:enddateStr];
                }
                
                
                  dyear=curYear-i/8640;
                      
                
                if (i%6==0) {
                    UILabel *danweiLabel=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 80, 20)];
                      [danweiLabel setTextColor:[UIColor lightGrayColor]];
                    [danweiLabel setTextAlignment:UITextAlignmentCenter];
                    [danweiLabel setBackgroundColor:[UIColor clearColor]];
                    [danweiLabel.text sizeWithFont:[UIFont systemFontOfSize:3]];
                    
                    danweiLabel.text=[NSString stringWithFormat:@"%d日%d时",dday,dhour];
                    [unitView addSubview:danweiLabel];
                   
                    [danweiLabel release];
                }

               
                NSString *yearKey = [NSString stringWithFormat:@"year%d", dyear];
                NSMutableDictionary *yearDict = [self.timelineDataDict objectForKey:yearKey];
                if (yearDict != nil) {
                    
                    NSString *monthKey = [NSString stringWithFormat:@"month%d", dmonth];
                    if (dmonth<10) {
                        monthKey=[NSString stringWithFormat:@"month0%d", dmonth];
                    }
                    NSMutableDictionary *monthDict = [yearDict objectForKey:monthKey];
                    if (monthDict != nil) {
                        
                        NSString *dayKey = [NSString stringWithFormat:@"day%d", dday];
                        if (dday<10) {
                            dayKey=[NSString stringWithFormat:@"day0%d", dday];
                        }
                        NSMutableDictionary *dayDict = [monthDict objectForKey:dayKey];
                        if (dayDict != nil) {
                            
                            NSString *hourKey = [NSString stringWithFormat:@"hour%d", dhour];
                            if (dhour<10) {
                                hourKey = [NSString stringWithFormat:@"hour0%d", dhour];
                                
                            }
                            
                            NSMutableDictionary *hourDict = [dayDict objectForKey:hourKey];
                            if (hourDict != nil) {
                              
                               
                                    if([[hourDict objectForKey:@"diarys"] count]!=0)   {
                                        
                                      //  unitView.backgroundColor=[UIColor redColor];
                                        [unitView setUserInteractionEnabled:YES];
                                        [unitView setMultipleTouchEnabled:YES];
                                        //UIButton *button=[[UIButton alloc]initWithFrame:CGRectMake(0, 0, 50, 40)];
                                        
                                        UIButton *button=[[UIButton alloc]initWithFrame:CGRectMake(unitView.frame.origin.x-unitView.frame.size.width/4, unitView.frame.origin.y, unitView.frame.size.width/2, unitView.frame.size.height)];
                                        
                                        button.tag=[[[[hourDict objectForKey:@"diarys"] objectAtIndex:0] objectForKey:@"id"] intValue];
                                        
                                        
                                        button.userInteractionEnabled=YES;
                                        [button addTarget:self action:@selector(toDiaryView:) forControlEvents:UIControlEventTouchUpInside];
                                        [button setBackgroundColor:[UIColor clearColor]];
                                        UIImageView *poitImageView = [[UIImageView alloc] initWithFrame:CGRectMake(unitViewWidth/2-12, height/2-10, 6, 6)];
                                        [poitImageView setImage:[UIImage imageNamed:@"point_3.png"]];
                                       // [button addSubview:poitImageView];
                                        [unitView addSubview:poitImageView];
                                        [poitImageView release];


                                        [button setCenter:CGPointMake((unitViewWidth/2)*i*scrolValue, (100 -height) /2)];
                                        [myScrollView addSubview:button];
                                        [scrollSubViews setObject:button forKey:[NSString stringWithFormat:@"button%d",i]];
                                        [button release];

                                        
                                    }
                                                                  
                            }
                            
                            
                        }
                        
                        
                    }
                    
                    
                }
                NSString *unitViewXPosition = [NSString stringWithFormat:@"unit%d", i];
                
                [scrollSubViews setObject:unitView forKey:unitViewXPosition];
            }//category
            
            
            if(unitCategory==@"day")
            {
                
                if (i %5 == 0) {
                    height = 45;
                }
                else{
                    height = 30;
                }
                UnitView *unitView = [[UnitView alloc] initWithFrame:CGRectMake(0, 0, unitViewWidth*scrolValue, height)];
                [unitView setCenter:CGPointMake(10*i*scrolValue, (100 -height) /2)];
                [scrollBaseView addSubview:unitView];
                

                dday=30-i%30;
                if (dday==0) {
                    dday=30;
                }
                
                dmonth=abs(curMonth-i/30)%12;
                if (dmonth==0) {
                    dmonth=12;
                }
                if (curMonth-i/30<0) {
                    dmonth=12-abs(curMonth-i/30)%12;
                }
               
                dyear=curYear-i/360;
                 if((currentTime - i*60*60*24) < rulerBeginFlag/1000) {  //a month later
                    //刷新数据 
                    
                    //begindate=begindate-86400000*30; 
                    begindate=begindate-86400000*10;
                     begindate=begindate-86400000*10;
                     begindate=begindate-86400000*10;
                    
                    //enddate = enddate-86400000*30; 
                      enddate = enddate-86400000*10; 
                     enddate = enddate-86400000*10; 
                     enddate = enddate-86400000*10; 
                     
                     rulerBeginFlag = begindate;
                    
                    NSString *enddateStr = [NSString stringWithFormat:@"%lld", enddate];
                    NSString *begindateStr = [NSString stringWithFormat:@"%lld", begindate];
                    
                    [self loadRulerDataWith:begindateStr endtime:enddateStr];
                }
                
                
                if (i%5==0) {
                    UILabel *danweiLabel=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 80, 20)];
                      [danweiLabel setTextColor:[UIColor lightGrayColor]];
                    [danweiLabel setTextAlignment:UITextAlignmentCenter];
                    [danweiLabel setBackgroundColor:[UIColor clearColor]];
                    [danweiLabel.text sizeWithFont:[UIFont systemFontOfSize:1]];
                    
                    danweiLabel.text=[NSString stringWithFormat:@"%d月%d日",dmonth,dday];
                  
                    [unitView addSubview:danweiLabel];
                   
                    [danweiLabel release];
                }

                NSString *yearKey = [NSString stringWithFormat:@"year%d", dyear];
                NSMutableDictionary *yearDict = [self.timelineDataDict objectForKey:yearKey];
                if (yearDict != nil) {
                    
                    NSString *monthKey = [NSString stringWithFormat:@"month%d", dmonth];
                    if (dmonth<10) {
                        monthKey=[NSString stringWithFormat:@"month0%d", dmonth];
                    }
                    NSMutableDictionary *monthDict = [yearDict objectForKey:monthKey];
                    if (monthDict != nil) {
                        
                        NSString *dayKey = [NSString stringWithFormat:@"day%d", dday];
                        if (dday<10) {
                            dayKey=[NSString stringWithFormat:@"day0%d", dday];
                        }
                        NSMutableDictionary *dayDict = [monthDict objectForKey:dayKey];
                        if (dayDict != nil) {
                            
                                                           
                                if([[dayDict objectForKey:@"diarys"] count]!=0)   {
                                    
                                    //unitView.backgroundColor=[UIColor redColor];
                                    [unitView setUserInteractionEnabled:YES];
                                    [unitView setMultipleTouchEnabled:YES];
                                    //UIButton *button=[[UIButton alloc]initWithFrame:CGRectMake(0, 0, 50, 40)];
                                    
                                    UIButton *button=[[UIButton alloc]initWithFrame:CGRectMake(unitView.frame.origin.x-unitView.frame.size.width/4, unitView.frame.origin.y, unitView.frame.size.width/2, unitView.frame.size.height)];
                                    
                                    button.tag=[[[[dayDict objectForKey:@"diarys"] objectAtIndex:0] objectForKey:@"id"] intValue];
                                    
                                    
                                    button.userInteractionEnabled=YES;
                                    [button addTarget:self action:@selector(toDiaryView:) forControlEvents:UIControlEventTouchUpInside];
                                    [button setBackgroundColor:[UIColor clearColor]];
                                    UIImageView *poitImageView = [[UIImageView alloc] initWithFrame:CGRectMake(unitViewWidth/2-12, height/2-10, 6, 6)];
                                    [poitImageView setImage:[UIImage imageNamed:@"point_3.png"]];
                                  //  [button addSubview:poitImageView];
                                    [unitView addSubview:poitImageView];
                                    [poitImageView release];

                                    [button setCenter:CGPointMake((unitViewWidth/2)*i*scrolValue, (100 -height) /2)];
                                    [myScrollView addSubview:button];
                                    [scrollSubViews setObject:button forKey:[NSString stringWithFormat:@"button%d",i]];
                                    [button release];

                                    
                                }
                                                       
                        }
                        
                        
                    }
                    
                    
                }
                NSString *unitViewXPosition = [NSString stringWithFormat:@"unit%d", i];
                
                [scrollSubViews setObject:unitView forKey:unitViewXPosition];
            }//category
            
            if(unitCategory==@"month")
            {
                if (i %3 == 0) {
                    height = 45;
                }
                else{
                    height = 30;
                }
                UnitView *unitView = [[UnitView alloc] initWithFrame:CGRectMake(0, 0, unitViewWidth*scrolValue, height)];
                [unitView setCenter:CGPointMake(10*i*scrolValue, (100 -height) /2)];
                [scrollBaseView addSubview:unitView];
                
                dmonth=12-i%12;
                if (dmonth==0) {
                    dmonth=12;
                }
                if (i<0) {
                    dmonth=12-abs(i)%12;
                }
                dyear=curYear-i/12;
                   
                 if((currentTime - i*60*60*24*30) < rulerBeginFlag/1000) {    // 30 month later
                    //刷新数据
                    
                    begindate=begindate/1000-86400*30*30; 
                    begindate=begindate*1000;
                    
                  //  enddate = enddate-86400000*30*30; 
                     enddate = enddate/1000-86400*30*30; 
                     enddate = enddate*1000;
                     
                     rulerBeginFlag = begindate;
                    
                    NSString *enddateStr = [NSString stringWithFormat:@"%lld", enddate];
                    NSString *begindateStr = [NSString stringWithFormat:@"%lld", begindate];
                    
                    [self loadRulerDataWith:begindateStr endtime:enddateStr];
                                        
                }

                
                if (i%3==0) {
                    UILabel *danweiLabel=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 20)];
                      [danweiLabel setTextColor:[UIColor lightGrayColor]];
                    [danweiLabel setTextAlignment:UITextAlignmentLeft];
                    [danweiLabel setBackgroundColor:[UIColor clearColor]];
                    [danweiLabel.text sizeWithFont:[UIFont systemFontOfSize:1]];
                    
                    danweiLabel.text=[NSString stringWithFormat:@"%d年%d月",dyear,dmonth];
                   // [danweiLabel setCenter:CGPointMake(10*i*scrolValue, 10)];
                    [unitView addSubview:danweiLabel];
                   
                    [danweiLabel release];
                }

                
                NSString *yearKey = [NSString stringWithFormat:@"year%d", dyear];
                NSMutableDictionary *yearDict = [self.timelineDataDict objectForKey:yearKey];
                if (yearDict != nil) {
                    
                    NSString *monthKey = [NSString stringWithFormat:@"month%d", dmonth];
                    if (dmonth<10) {
                        monthKey=[NSString stringWithFormat:@"month0%d", dmonth];
                    }
                    NSMutableDictionary *monthDict = [yearDict objectForKey:monthKey];
                    if (monthDict != nil) {
                        
                                                       
                                //unitView.backgroundColor=[UIColor redColor];
                                [unitView setUserInteractionEnabled:YES];
                                [unitView setMultipleTouchEnabled:YES];
                                //UIButton *button=[[UIButton alloc]initWithFrame:CGRectMake(0, 0, 50, 40)];
                                
                                UIButton *button=[[UIButton alloc]initWithFrame:CGRectMake(unitView.frame.origin.x-unitView.frame.size.width/4, unitView.frame.origin.y, unitView.frame.size.width/2, unitView.frame.size.height)];
                                
                                button.tag=[[[[monthDict objectForKey:@"diarys"] objectAtIndex:0] objectForKey:@"id"] intValue];
                                
                                
                                button.userInteractionEnabled=YES;
                                [button addTarget:self action:@selector(toDiaryView:) forControlEvents:UIControlEventTouchUpInside];
                        [button setBackgroundColor:[UIColor clearColor]];
                        UIImageView *poitImageView = [[UIImageView alloc] initWithFrame:CGRectMake(unitViewWidth/2-12, height/2-10, 6, 6)];
                        [poitImageView setImage:[UIImage imageNamed:@"point_3.png"]];
                       // [button addSubview:poitImageView];
                        [unitView addSubview:poitImageView];
                        [poitImageView release];


                                [button setCenter:CGPointMake((unitViewWidth/2)*i*scrolValue, (100 -height) /2)];
                                [myScrollView addSubview:button];
                                
                        [scrollSubViews setObject:button forKey:[NSString stringWithFormat:@"button%d",i]];
                        [button release];

                      
                        }
                      
                    
                }
                NSString *unitViewXPosition = [NSString stringWithFormat:@"unit%d", i];
                
                [scrollSubViews setObject:unitView forKey:unitViewXPosition];
            }
            
            CLog(@"i %d", i);
            CLog(@"dyear%d dmonth%d dday%d dhour%d dmin%d", dyear,dmonth,dday,dhour,dminute);
          
        }
        indexUp = newIndexUP;

    }
    //是否超出当前显示下限
    if (newIndexDown < indexDown && newIndexUP > indexDown) {
        
  
        for (int i = indexDown -1; i >= newIndexDown; i--) {
            
            if(unitCategory==@"min"){
                
                if (i %5 == 0) {
                    height = 45;
                }
                else{
                    height = 30;
                }
                UnitView *unitView = [[UnitView alloc] initWithFrame:CGRectMake(0, 0, unitViewWidth*scrolValue, height)];
                [unitView setCenter:CGPointMake(10*i*scrolValue, (100 -height) /2)];
                [scrollBaseView addSubview:unitView];
                
                
                dminute=60-i%60;
                if (dminute==60) {
                    dminute=0;
                }
                
                dhour=23-((i-1)/60)%24;
                             
                    dday=abs(self.curDay-i/1440)%30;
                    if (dday==0) {
                        dday=30;
                    }
                    
                    if (self.curDay-i/1440<0) {
                        dday=30-abs(self.curDay-i/1440)%30;
                    }
                                       
                    dmonth=abs(curMonth-i/43200)%12;
                    if (dmonth==0) {
                        dmonth=12;
                    }
                
                if (i<=0) {
                    dminute=(abs(i)%60);
                    dhour=(abs(i)/60%24);
                    dday=abs(self.curDay+1-abs(i)/1440)%30;
                    dmonth=abs(curMonth+1-i/43200)%12;
                }
                               
                    if (curMonth-i/43200<0) {
                        dmonth=12-abs(curMonth-i/43200)%12;
                    }
                    
                    
                
                if (i%518400==0) {
                    dyear=curYear-i/518400;
                }
                
                
                if (i%10==0) {
                    UILabel *danweiLabel=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 80, 20)];
                      [danweiLabel setTextColor:[UIColor lightGrayColor]];
                    [danweiLabel setTextAlignment:UITextAlignmentLeft];
                    [danweiLabel setBackgroundColor:[UIColor clearColor]];
                    [danweiLabel.text sizeWithFont:[UIFont systemFontOfSize:4]];
                    
                    danweiLabel.text=[NSString stringWithFormat:@"%d:%d",dhour,dminute];

                    [unitView addSubview:danweiLabel];
                   
                    [danweiLabel release];
                }
                
                
                
                
                NSString *yearKey = [NSString stringWithFormat:@"year%d", dyear];
                NSMutableDictionary *yearDict = [self.timelineDataDict objectForKey:yearKey];
                if (yearDict != nil) {
                    
                    NSString *monthKey = [NSString stringWithFormat:@"month%d", dmonth];
                    if (dmonth<10) {
                        monthKey=[NSString stringWithFormat:@"month0%d", dmonth];
                    }
                    NSMutableDictionary *monthDict = [yearDict objectForKey:monthKey];
                    if (monthDict != nil) {
                        
                        NSString *dayKey = [NSString stringWithFormat:@"day%d", dday];
                        if (dday<10) {
                            dayKey=[NSString stringWithFormat:@"day0%d", dday];
                        }
                        NSMutableDictionary *dayDict = [monthDict objectForKey:dayKey];
                        if (dayDict != nil) {
                            
                            NSString *hourKey = [NSString stringWithFormat:@"hour%d", dhour];
                            if (dhour<10) {
                                hourKey = [NSString stringWithFormat:@"hour0%d", dhour];
                                
                            }
                            
                            NSMutableDictionary *hourDict = [dayDict objectForKey:hourKey];
                            if (hourDict != nil) {
                                
                                NSString *minKey = [NSString stringWithFormat:@"min%d", dminute];
                                if (i<10) {
                                    minKey=[NSString stringWithFormat:@"min0%d", dminute];
                                }
                                NSMutableDictionary *minDict = [hourDict objectForKey:minKey];
                                if (minDict != nil) {
                                    if([[minDict objectForKey:@"diarys"] count]!=0)   {
                                        
                                        //unitView.backgroundColor=[UIColor redColor];
                                        [unitView setUserInteractionEnabled:YES];
                                        [unitView setMultipleTouchEnabled:YES];
                                        //UIButton *button=[[UIButton alloc]initWithFrame:CGRectMake(0, 0, 50, 40)];
                                        
                                        UIButton *button=[[UIButton alloc]initWithFrame:CGRectMake(unitView.frame.origin.x-unitView.frame.size.width/4, unitView.frame.origin.y, unitView.frame.size.width/2, unitView.frame.size.height)];
                                        
                                        button.tag=[[[[minDict objectForKey:@"diarys"] objectAtIndex:0] objectForKey:@"id"] intValue];
                                        
                                        
                                        button.userInteractionEnabled=YES;
                                        [button addTarget:self action:@selector(toDiaryView:) forControlEvents:UIControlEventTouchUpInside];
                                        [button setBackgroundColor:[UIColor clearColor]];
                                        UIImageView *poitImageView = [[UIImageView alloc] initWithFrame:CGRectMake (unitViewWidth/2-12, height/2-10, 6, 6)];
                                        [poitImageView setImage:[UIImage imageNamed:@"point_3.png"]];
                                       // [button addSubview:poitImageView];
                                        [unitView addSubview:poitImageView];
                                        [poitImageView release];


                                        [button setCenter:CGPointMake((unitViewWidth/2)*i*scrolValue, (100 -height) /2)];
                                        [myScrollView addSubview:button];
                                        [scrollSubViews setObject:button forKey:[NSString stringWithFormat:@"button%d",i]];
                                        [button release];
                                        
                                        
                                    }
                                    
                                }
                                
                            }
                            
                            
                        }
                        
                        
                    }
                    
                    
                }
                NSString *unitViewXPosition = [NSString stringWithFormat:@"unit%d", i];
                
                [scrollSubViews setObject:unitView forKey:unitViewXPosition];
            }//category
            
            if(unitCategory==@"hour")
            {
                
                
                if (i %3 == 0) {
                    height = 45;
                }
                else{
                    height = 30;
                }
                UnitView *unitView = [[UnitView alloc] initWithFrame:CGRectMake(0, 0, unitViewWidth*scrolValue, height)];
                [unitView setCenter:CGPointMake(10*i*scrolValue, (100 -height) /2)];
                [scrollBaseView addSubview:unitView];
                
                dhour=24-i%24;
                if (dhour==24) {
                    dhour=0;
                }
               
                    self.dday=abs(curDay-(i-1)/24)%30;
                    if (dday==0) {
                        dday=30;
                    }
                    if (curDay-i/24<0) {
                        dday=30-abs(curDay-i/24)%30;
                    }
                    
                if (i<=0) {
                    dhour=abs(i)%24;
                      self.dday=abs(curDay+1-i/24)%30;
                }              
                    
                    dmonth=abs(curMonth-i/720)%12;
                    if (dmonth==0) {
                        dmonth=12;
                        if (curMonth-i/720<0) {
                            dmonth=12-abs(curMonth-i/720)%12;
                        }
                   
                                    }
              
                    dyear=curYear-i/8640;
               
               
                if (i%6==0) {
                    UILabel *danweiLabel=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 80, 20)];
                      [danweiLabel setTextColor:[UIColor lightGrayColor]];
                    [danweiLabel setTextAlignment:UITextAlignmentCenter];
                    [danweiLabel setBackgroundColor:[UIColor clearColor]];
                    [danweiLabel.text sizeWithFont:[UIFont systemFontOfSize:3]];
                    
                    danweiLabel.text=[NSString stringWithFormat:@"%d日%d时",self.dday,dhour];
                                     [unitView addSubview:danweiLabel];
                   
                    [danweiLabel release];
                }
               
                
                NSString *yearKey = [NSString stringWithFormat:@"year%d", dyear];
                NSMutableDictionary *yearDict = [self.timelineDataDict objectForKey:yearKey];
                if (yearDict != nil) {
                    
                    NSString *monthKey = [NSString stringWithFormat:@"month%d", dmonth];
                    if (dmonth<10) {
                        monthKey=[NSString stringWithFormat:@"month0%d", dmonth];
                    }
                    NSMutableDictionary *monthDict = [yearDict objectForKey:monthKey];
                    if (monthDict != nil) {
                        
                        NSString *dayKey = [NSString stringWithFormat:@"day%d", dday];
                        if (dday<10) {
                            dayKey=[NSString stringWithFormat:@"day0%d", dday];
                        }
                        NSMutableDictionary *dayDict = [monthDict objectForKey:dayKey];
                        if (dayDict != nil) {
                            
                            NSString *hourKey = [NSString stringWithFormat:@"hour%d", dhour];
                            if (dhour<10) {
                                hourKey = [NSString stringWithFormat:@"hour0%d", dhour];
                                
                            }
                            
                            NSMutableDictionary *hourDict = [dayDict objectForKey:hourKey];
                            if (hourDict != nil) {
                                
                                
                                if([[hourDict objectForKey:@"diarys"] count]!=0)   {
                                    
                                    //unitView.backgroundColor=[UIColor redColor];
                                    [unitView setUserInteractionEnabled:YES];
                                    [unitView setMultipleTouchEnabled:YES];
                                    //UIButton *button=[[UIButton alloc]initWithFrame:CGRectMake(0, 0, 50, 40)];
                                    
                                    UIButton *button=[[UIButton alloc]initWithFrame:CGRectMake(unitView.frame.origin.x-unitView.frame.size.width/4, unitView.frame.origin.y, unitView.frame.size.width/2, unitView.frame.size.height)];                                    
                                    button.tag=[[[[hourDict objectForKey:@"diarys"] objectAtIndex:0] objectForKey:@"id"] intValue];
                                    
                                    
                                    button.userInteractionEnabled=YES;
                                    [button addTarget:self action:@selector(toDiaryView:) forControlEvents:UIControlEventTouchUpInside];
                                    [button setBackgroundColor:[UIColor clearColor]];
                                    UIImageView *poitImageView = [[UIImageView alloc] initWithFrame:CGRectMake(-2, -4, 6, 6)];
                                    [poitImageView setImage:[UIImage imageNamed:@"point_3.png"]];
                                 //  [button addSubview:poitImageView];
                                   // [unitView addSubview:poitImageView];
                                    [unitView.lineView addSubview:poitImageView];
                                    
                                    [poitImageView release];

                                    [button setCenter:CGPointMake((unitViewWidth/2)*i*scrolValue, (100 -height) /2)];
                                    [button setCenter:unitView.center];
                                    [myScrollView addSubview:button];
                                    [scrollSubViews setObject:button forKey:[NSString stringWithFormat:@"button%d",i]];
                                    [button release];
                                    
                                    
                                }
                                
                            }
                            
                            
                        }
                        
                        
                    }
                    
                    
                }
                NSString *unitViewXPosition = [NSString stringWithFormat:@"unit%d", i];
                
                [scrollSubViews setObject:unitView forKey:unitViewXPosition];
            }//category
            
            
            if(unitCategory==@"day")
            {
                
                if (i %5 == 0) {
                    height = 45;
                }
                else{
                    height = 30;
                }
                UnitView *unitView = [[UnitView alloc] initWithFrame:CGRectMake(0, 0, unitViewWidth*scrolValue, height)];
                [unitView setCenter:CGPointMake(10*i*scrolValue, (100 -height) /2)];
                [scrollBaseView addSubview:unitView];
                
                
                dday=30-i%30;
                if (dday==0) {
                    dday=30;
                }
                                
                
                    
                    dmonth=abs(curMonth-i/30)%12;
                    if (dmonth==0) {
                        dmonth=12;
                    }
                    if (curMonth-i/30<0) {
                        dmonth=12-abs(curMonth-i/30)%12;
                    }
               
              
                    dyear=curYear-i/360;
                                      
                if (i<0) {
                    
                    dmonth=abs(curMonth+1-i/30)%12;

                }
                if (i==0) {
                    dmonth=curMonth;
                }

              
                
                if (i%5==0) {
                    UILabel *danweiLabel=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 80, 20)];
                      [danweiLabel setTextColor:[UIColor lightGrayColor]];
                    [danweiLabel setTextAlignment:UITextAlignmentCenter];
                    [danweiLabel setBackgroundColor:[UIColor clearColor]];
                    [danweiLabel.text sizeWithFont:[UIFont systemFontOfSize:1]];
                    
                    danweiLabel.text=[NSString stringWithFormat:@"%d月%d日",dmonth,dday];
                   
                    [unitView addSubview:danweiLabel];
                                       [danweiLabel release];
                }
                
                
                
                
                NSString *yearKey = [NSString stringWithFormat:@"year%d", dyear];
                NSMutableDictionary *yearDict = [self.timelineDataDict objectForKey:yearKey];
                if (yearDict != nil) {
                    
                    NSString *monthKey = [NSString stringWithFormat:@"month%d", dmonth];
                    if (dmonth<10) {
                        monthKey=[NSString stringWithFormat:@"month0%d", dmonth];
                    }
                    NSMutableDictionary *monthDict = [yearDict objectForKey:monthKey];
                    if (monthDict != nil) {
                        
                        NSString *dayKey = [NSString stringWithFormat:@"day%d", dday];
                        if (dday<10) {
                            dayKey=[NSString stringWithFormat:@"day0%d", dday];
                        }
                        NSMutableDictionary *dayDict = [monthDict objectForKey:dayKey];
                        if (dayDict != nil) {
                            
                            
                            if([[dayDict objectForKey:@"diarys"] count]!=0)   {
                                
                               // unitView.backgroundColor=[UIColor redColor];
                                [unitView setUserInteractionEnabled:YES];
                                [unitView setMultipleTouchEnabled:YES];
                                //UIButton *button=[[UIButton alloc]initWithFrame:CGRectMake(0, 0, 50, 40)];
                                
                                UIButton *button=[[UIButton alloc]initWithFrame:CGRectMake(unitView.frame.origin.x-unitView.frame.size.width/4, unitView.frame.origin.y, unitView.frame.size.width/2, unitView.frame.size.height)];
                                
                                button.tag=[[[[dayDict objectForKey:@"diarys"] objectAtIndex:0] objectForKey:@"id"] intValue];
                                
                                
                                button.userInteractionEnabled=YES;
                                [button addTarget:self action:@selector(toDiaryView:) forControlEvents:UIControlEventTouchUpInside];
                                [button setBackgroundColor:[UIColor clearColor]];
                                UIImageView *poitImageView = [[UIImageView alloc] initWithFrame:CGRectMake(-2, -4, 6, 6)];
                                [poitImageView setImage:[UIImage imageNamed:@"point_3.png"]];
                               // [button addSubview:poitImageView];
                                [unitView.lineView addSubview:poitImageView];
                        
                                [poitImageView release];


                                [button setCenter:CGPointMake((unitViewWidth/2)*i*scrolValue, (100 -height) /2)];
                                [myScrollView addSubview:button];
                                [scrollSubViews setObject:button forKey:[NSString stringWithFormat:@"button%d",i]];
                                [button release];
                                
                                
                            }
                            
                            
                            
                            
                        }
                        
                        
                    }
                    
                    
                }
                NSString *unitViewXPosition = [NSString stringWithFormat:@"unit%d", i];
                
                [scrollSubViews setObject:unitView forKey:unitViewXPosition];
            }//category
            
            if(unitCategory==@"month")
            {
                if (i %3 == 0) {
                    height = 45;
                }
                else{
                    height = 30;
                }
                UnitView *unitView = [[UnitView alloc] initWithFrame:CGRectMake(0, 0, unitViewWidth*scrolValue, height)];
                [unitView setCenter:CGPointMake(10*i*scrolValue, (100 -height) /2)];
                [scrollBaseView addSubview:unitView];
                
                dmonth=12-i%12;
                if (dmonth==0) {
                    dmonth=12;
                }
                               
                 dyear=curYear-i/12;
                
                if (i==0) {
                    dmonth=12;
                    dyear=curYear;
                }
                
                if (i<0) {
                    dmonth=abs(i)%12;
                    dyear=curYear+1-i/12;
                }

                                             

                if (i%3==0) {
                    UILabel *danweiLabel=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 20)];
                      [danweiLabel setTextColor:[UIColor lightGrayColor]];
                    [danweiLabel setTextAlignment:UITextAlignmentLeft];
                    [danweiLabel setBackgroundColor:[UIColor clearColor]];
                    [danweiLabel.text sizeWithFont:[UIFont systemFontOfSize:1]];
                    
                    danweiLabel.text=[NSString stringWithFormat:@"%d年%d月",dyear,dmonth];
                                     [unitView addSubview:danweiLabel];
                   
                    [danweiLabel release];
                }
                
                
                
                NSString *yearKey = [NSString stringWithFormat:@"year%d", dyear];
                NSMutableDictionary *yearDict = [self.timelineDataDict objectForKey:yearKey];
                if (yearDict != nil) {
                    
                    NSString *monthKey = [NSString stringWithFormat:@"month%d", dmonth];
                    if (dmonth<10) {
                        monthKey=[NSString stringWithFormat:@"month0%d", dmonth];
                    }
                    NSMutableDictionary *monthDict = [yearDict objectForKey:monthKey];
                    if (monthDict != nil) {
                        
                        
                        //unitView.backgroundColor=[UIColor redColor];
                        [unitView setUserInteractionEnabled:YES];
                        [unitView setMultipleTouchEnabled:YES];
                        //UIButton *button=[[UIButton alloc]initWithFrame:CGRectMake(0, 0, 50, 40)];
                        
                        UIButton *button=[[UIButton alloc]initWithFrame:CGRectMake(unitView.frame.origin.x-unitView.frame.size.width/4, unitView.frame.origin.y, unitView.frame.size.width/2, unitView.frame.size.height)];
                        
                        button.tag=[[[[monthDict objectForKey:@"diarys"] objectAtIndex:0] objectForKey:@"id"] intValue];
                        
                        
                        button.userInteractionEnabled=YES;
                        [button addTarget:self action:@selector(toDiaryView:) forControlEvents:UIControlEventTouchUpInside];
                        [button setBackgroundColor:[UIColor clearColor]];
                        UIImageView *poitImageView = [[UIImageView alloc] initWithFrame:CGRectMake(-2, -4, 6, 6)];
                        [poitImageView setImage:[UIImage imageNamed:@"point_3.png"]];
                        // [button addSubview:poitImageView];
                        [unitView.lineView addSubview:poitImageView];                      
                        [poitImageView release];


                        [button setCenter:CGPointMake((unitViewWidth/2)*i*scrolValue, (100 -height) /2)];
                        [myScrollView addSubview:button];
                        
                        [scrollSubViews setObject:button forKey:[NSString stringWithFormat:@"button%d",i]];
                        [button release];
                        
                        
                        
                        
                        
                    }
                    
                    
                }
                NSString *unitViewXPosition = [NSString stringWithFormat:@"unit%d", i];
                
                [scrollSubViews setObject:unitView forKey:unitViewXPosition];
            }
            
            
//            CLog(@"i %d", i);
//             CLog(@"dyear%d dmonth%d dday%d dhour%d dmin%d", dyear,dmonth,dday,dhour,dminute);
            
            
        }
        indexDown = newIndexDown;
        
    }
    
    
    
    
    
        
    //NSLog(@"scroll view center %f,%f",pointX, pointY);
    
    //重设ContentSize
    float newContentSize = pointX +640;
    CGSize size = scrollView.contentSize;
    if (newContentSize > size.width) {
        
        [scrollView setContentSize:CGSizeMake(newContentSize, size.height)];
    }

}
// any offset changes
- (void)scrollViewDidZoom:(UIScrollView *)scrollView __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_3_2)
{
    
    UIView *zoomview = scrollBaseView;
    zoomview.transform = CGAffineTransformMakeScale(scrollView.zoomScale, 1); 
    zoomview.center = CGPointMake(zoomview.frame.size.width /2, zoomview.frame.size.height /2);
   
    //zoomview.frame = CGRectMake(0, 0, 320 *scrollView.zoomScale, 50);
    //scrolValue = scrollView.zoomScale;
    
    NSLog(@"/nScroll View did zoom!  Scrol %f",scrollView.zoomScale);
        
    
}// any zoom scale changes


-(void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale{
    
    if (unitCategory == @"min") {
        if (scale>1) {
            
            [myScrollView setZoomScale:1];
            scrollBaseView.transform=CGAffineTransformMakeScale(1, 1);
            scrollBaseView.center = CGPointMake(scrollBaseView.frame.size.width /2, scrollBaseView.frame.size.height /2);
            return;
        }
    }
    
    if (unitCategory == @"month") {
        if (scale<1) {
            
            [myScrollView setZoomScale:1];
            scrollBaseView.transform=CGAffineTransformMakeScale(1, 1);
            scrollBaseView.center = CGPointMake(scrollBaseView.frame.size.width /2, scrollBaseView.frame.size.height /2);
            return;
        }
    }

    
    
    self.lastCategory=self.unitCategory;
    lastScrolValue=scrolValue;
    
    _isFirstIn = YES;
    [LoadingView showDefaultLoadingView];
    
    if(scale>1){
        if((unitCategory==@"hour"||unitCategory==@"min")&&!scaleIsChange){
            scrolValue=1;
            lineCount=60;
        
            scaleIsChange=YES;
            unitCategory=@"min";
            
            
            //重新生成尺子
            [self setTimelineRuler];
        
        //刷新数据
        NSTimeInterval time = [[NSDate date] timeIntervalSince1970];
        long long int longtime = (long long int)time*1000; 
    
        begindate=longtime-86400000*7; 
        
        enddate = longtime; 
        
        
        NSString *enddateStr = [NSString stringWithFormat:@"%lld", enddate];
        NSString *begindateStr = [NSString stringWithFormat:@"%lld", begindate];
        
        [self loadRulerDataWith:begindateStr endtime:enddateStr];
        
        return;
    }    
        
    if(unitCategory==@"day"&&!scaleIsChange){
        
        scrolValue=2;
        lineCount=40;
        scaleIsChange=YES;
        unitCategory=@"hour";
        
        
        //重新生成尺子
        [self setTimelineRuler];
       
        //刷新数据
        NSTimeInterval time = [[NSDate date] timeIntervalSince1970];
        long long int longtime = (long long int)time*1000; 
        
        begindate=longtime-86400000*7; 
        
        enddate = longtime; 
        
        
        NSString *enddateStr = [NSString stringWithFormat:@"%lld", enddate];
        NSString *begindateStr = [NSString stringWithFormat:@"%lld", begindate];
        
        [self loadRulerDataWith:begindateStr endtime:enddateStr];

        return;
    }
        
    if(unitCategory==@"month"&&!scaleIsChange){
         scrolValue=3;
         lineCount=30;
        scaleIsChange=YES;
        unitCategory=@"day";
        
        
        //重新生成尺子
        [self setTimelineRuler];
        
        //刷新数据
        NSTimeInterval time = [[NSDate date] timeIntervalSince1970];
        long long int longtime = (long long int)time*1000; 
        
        begindate=longtime-86400000*10;   //居然不能一次减去这么大的数 
        begindate=begindate-86400000*10; 
        begindate=begindate-86400000*10; 
        
        enddate = longtime; 
        
        
        NSString *enddateStr = [NSString stringWithFormat:@"%lld", enddate];
        NSString *begindateStr = [NSString stringWithFormat:@"%lld", begindate];
        
        [self loadRulerDataWith:begindateStr endtime:enddateStr];

        return;
    }
        
    if((unitCategory==@"year"||unitCategory==@"month")&&!scaleIsChange){
        scrolValue=5;
        lineCount=20;
        scaleIsChange=YES;
        unitCategory=@"month";
        
        
        //重新生成尺子
        [self setTimelineRuler];
        
        //刷新数据
        NSTimeInterval time = [[NSDate date] timeIntervalSince1970];
        long long int longtime = (long long int)time; 
        
        begindate=longtime-86400*30*30;   //居然不能一次减去这么大的数 
        enddate = longtime; 
        
         begindate = begindate*1000;
        enddate = enddate*1000;
        
        NSString *enddateStr = [NSString stringWithFormat:@"%lld", enddate];
        NSString *begindateStr = [NSString stringWithFormat:@"%lld", begindate];
        
        [self loadRulerDataWith:begindateStr endtime:enddateStr];
        return;
    }
   
}
    
    if (scale<=1) {
        if(unitCategory==@"min"&&!scaleIsChange){
            scrolValue=2;
            lineCount=40;
            scaleIsChange=YES;
            unitCategory=@"hour";
            
            
            //重新生成尺子
            [self setTimelineRuler];
            
            //刷新数据
            NSTimeInterval time = [[NSDate date] timeIntervalSince1970];
            long long int longtime = (long long int)time*1000; 
            
            begindate=longtime-86400000*7; 
            
            enddate = longtime; 
                        
            NSString *enddateStr = [NSString stringWithFormat:@"%lld", enddate];
            NSString *begindateStr = [NSString stringWithFormat:@"%lld", begindate];
            
            [self loadRulerDataWith:begindateStr endtime:enddateStr];
            
            return;
        }
        
        if(unitCategory==@"hour"&&!scaleIsChange){
            scrolValue=3;
            lineCount=30;
            scaleIsChange=YES;
            unitCategory=@"day";
            
            
            //重新生成尺子
            [self setTimelineRuler];
            
            //刷新数据
            NSTimeInterval time = [[NSDate date] timeIntervalSince1970];
            long long int longtime = (long long int)time*1000; 
            
           
            begindate=longtime-86400000*10;   //居然不能一次减去这么大的数 
            begindate=begindate-86400000*10; 
            begindate=begindate-86400000*10; 
           
            
            enddate = longtime; 
            
            
            NSString *enddateStr = [NSString stringWithFormat:@"%lld", enddate];
            NSString *begindateStr = [NSString stringWithFormat:@"%lld", begindate];
            
            [self loadRulerDataWith:begindateStr endtime:enddateStr];
            
            return;
        }
        if((unitCategory==@"day"||unitCategory==@"month")&&!scaleIsChange){
            scrolValue=5;
            lineCount=20;
            scaleIsChange=YES;
            unitCategory=@"month";
            
            
            //重新生成尺子
            [self setTimelineRuler];
            
            //刷新数据
            NSTimeInterval time = [[NSDate date] timeIntervalSince1970];
            long long int longtime = (long long int)time; 
            
            begindate=longtime-86400*30*30; 
            enddate = longtime; 
            
            begindate = begindate*1000;
            enddate = enddate*1000;
            
            
            NSString *enddateStr = [NSString stringWithFormat:@"%lld", enddate];
            NSString *begindateStr = [NSString stringWithFormat:@"%lld", begindate];
            
            [self loadRulerDataWith:begindateStr endtime:enddateStr];
            return;
        }
                
    }
    
    
    
}


// called on start of dragging (may require some time and or distance to move)
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    NSLog(@"Scroll View begin dragging!");
}
// called on finger up if the user dragged. velocity is in points/second. targetContentOffset may be changed to adjust where the scroll view comes to rest. not called when pagingEnabled is YES
//- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_5_0)
//{
//    NSLog(@"Scroll View will end dragging!");
//}
// called on finger up if the user dragged. decelerate is true if it will continue moving afterwards
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    
    
    CGPoint point = scrollView.contentOffset;
    int pointInt = point.x /(10*scrolValue);
    float pointX = pointInt *10*scrolValue;
    float pointY = point.y;
    
    
    NSInteger newIndexUP = pointInt +lineCount;
    NSInteger newIndexDown = pointInt -lineCount/2;
    
    float height = 0;
    //是否超出当前显示上限
    if (newIndexUP >indexUp) {
        
        
        for (int i = indexUp +1; i <= newIndexUP; i++) {
            
            if(unitCategory==@"min"){
                
                if (i %5 == 0) {
                    height = 45;
                }
                else{
                    height = 30;
                }
                UnitView *unitView = [[UnitView alloc] initWithFrame:CGRectMake(0, 0, unitViewWidth*scrolValue, height)];
                
                unitView.tag=10;
                [unitView setCenter:CGPointMake(10*i*scrolValue, (100 -height) /2)];
                [scrollBaseView addSubview:unitView];
                
                dminute=60-i%60;
                if (dminute==60) {
                    dminute=0;
                }
                
                dhour=23-((i-1)/60)%24;
                
                dday=abs(self.curDay-i/1440)%30;
                if (dday==0) {
                    dday=30;
                }
                
                if (self.curDay-i/1440<0) {
                    dday=30-abs(self.curDay-i/1440)%30;
                }
                
                if (i%(1440*7) == 0) {  //a week later
                    //刷新数据 
                    
                    begindate=begindate-86400000*7; 
                    
                    enddate = enddate-86400000*7; 
                    
                    NSString *enddateStr = [NSString stringWithFormat:@"%lld", enddate];
                    NSString *begindateStr = [NSString stringWithFormat:@"%lld", begindate];
                    
                    [self loadRulerDataWith:begindateStr endtime:enddateStr];
                }
                
                
                dmonth=abs(curMonth-i/43200)%12;
                
                if (dmonth==0) {
                    dmonth=12;
                }
                if (curMonth-i/43200<0) {
                    dmonth=12-abs(curMonth-i/43200)%12;
                }
                dyear=curYear-i/518400;
                
                
                if (i%10==0) {
                    UILabel *danweiLabel=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 80, 20)];
                      [danweiLabel setTextColor:[UIColor lightGrayColor]];
                    [danweiLabel setTextAlignment:UITextAlignmentLeft];
                    [danweiLabel setBackgroundColor:[UIColor clearColor]];
                    [danweiLabel.text sizeWithFont:[UIFont systemFontOfSize:4]];
                    
                    danweiLabel.text=[NSString stringWithFormat:@"%d:%d",dhour,dminute];
                    [unitView addSubview:danweiLabel];
                    [danweiLabel release];
                }
                
                
                NSString *yearKey = [NSString stringWithFormat:@"year%d", dyear];
                NSMutableDictionary *yearDict = [self.timelineDataDict objectForKey:yearKey];
                if (yearDict != nil) {
                    
                    NSString *monthKey = [NSString stringWithFormat:@"month%d", dmonth];
                    if (dmonth<10) {
                        monthKey=[NSString stringWithFormat:@"month0%d", dmonth];
                    }
                    NSMutableDictionary *monthDict = [yearDict objectForKey:monthKey];
                    if (monthDict != nil) {
                        
                        NSString *dayKey = [NSString stringWithFormat:@"day%d", dday];
                        if (dday<10) {
                            dayKey=[NSString stringWithFormat:@"day0%d", dday];
                        }
                        NSMutableDictionary *dayDict = [monthDict objectForKey:dayKey];
                        if (dayDict != nil) {
                            
                            NSString *hourKey = [NSString stringWithFormat:@"hour%d", dhour];
                            if (dhour<10) {
                                hourKey = [NSString stringWithFormat:@"hour0%d", dhour];
                                
                            }
                            
                            NSMutableDictionary *hourDict = [dayDict objectForKey:hourKey];
                            if (hourDict != nil) {
                                
                                NSString *minKey = [NSString stringWithFormat:@"min%d", dminute];
                                if (i<10) {
                                    minKey=[NSString stringWithFormat:@"min0%d", dminute];
                                }
                                NSMutableDictionary *minDict = [hourDict objectForKey:minKey];
                                if (minDict != nil) {
                                    if([[minDict objectForKey:@"diarys"] count]!=0)   {
                                        
                                        // unitView.backgroundColor=[UIColor redColor];
                                        [unitView setUserInteractionEnabled:YES];
                                        [unitView setMultipleTouchEnabled:YES];
                                        //UIButton *button=[[UIButton alloc]initWithFrame:CGRectMake(0, 0, 50, 40)];
                                        
                                        UIButton *button=[[UIButton alloc]initWithFrame:CGRectMake(unitView.frame.origin.x-unitView.frame.size.width/4, unitView.frame.origin.y, unitView.frame.size.width/2, unitView.frame.size.height)];
                                        
                                        button.tag=[[[[minDict objectForKey:@"diarys"] objectAtIndex:0] objectForKey:@"id"] intValue];
                                        
                                        
                                        button.userInteractionEnabled=YES;
                                        [button addTarget:self action:@selector(toDiaryView:) forControlEvents:UIControlEventTouchUpInside];
                                        [button setBackgroundColor:[UIColor clearColor]];
                                        UIImageView *poitImageView = [[UIImageView alloc] initWithFrame:CGRectMake(-2, -4, 6, 6)];
                                        [poitImageView setImage:[UIImage imageNamed:@"point_3.png"]];
                                        // [button addSubview:poitImageView];
                                        [unitView.lineView addSubview:poitImageView];
                                        [poitImageView release];
                                        
                                        [button setCenter:CGPointMake(10*i*scrolValue, (100 -height) /2)];
                                        [myScrollView addSubview:button];
                                        [scrollSubViews setObject:button forKey:[NSString stringWithFormat:@"button%d",i]];
                                        [button release];
                                        
                                        
                                    }
                                    
                                }
                                
                            }
                            
                            
                        }
                        
                        
                    }
                    
                    
                }
                NSString *unitViewXPosition = [NSString stringWithFormat:@"unit%d", i];
                
                [scrollSubViews setObject:unitView forKey:unitViewXPosition];
            }//category
            
            if(unitCategory==@"hour")
            {
                
                if (i %3 == 0) {
                    height = 45;
                }
                else{
                    height = 30;
                }
                UnitView *unitView = [[UnitView alloc] initWithFrame:CGRectMake(0, 0, unitViewWidth*scrolValue, height)];
                [unitView setCenter:CGPointMake(10*i*scrolValue, (100 -height) /2)];
                [scrollBaseView addSubview:unitView];
                
                
                dhour=24-i%24;
                if (dhour==24) {
                    dhour=0;
                }
                
                dday=abs(self.curDay-(i-1)/24)%30;
                if (dday==0) {
                    dday=30;
                }
                if (self.curDay-i/24<0) {
                    dday=30-abs(self.curDay-(i-1)/24)%30;
                }
                
                dmonth=abs(curMonth-i/720)%12;
                if (dmonth==0) {
                    dmonth=12;
                    
                }
                if (curMonth-i/720<0) {
                    dmonth=12-abs(curMonth-i/720)%12;
                }
                
                if (i%(24*7) == 0) {  //a week later
                    //刷新数据 
                    
                    begindate=begindate-86400000*7; 
                    
                    enddate = enddate-86400000*7; 
                    
                    NSString *enddateStr = [NSString stringWithFormat:@"%lld", enddate];
                    NSString *begindateStr = [NSString stringWithFormat:@"%lld", begindate];
                    
                    [self loadRulerDataWith:begindateStr endtime:enddateStr];
                }
                
                
                dyear=curYear-i/8640;
                
                
                if (i%6==0) {
                    UILabel *danweiLabel=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 80, 20)];
                      [danweiLabel setTextColor:[UIColor lightGrayColor]];
                    [danweiLabel setTextAlignment:UITextAlignmentCenter];
                    [danweiLabel setBackgroundColor:[UIColor clearColor]];
                    [danweiLabel.text sizeWithFont:[UIFont systemFontOfSize:3]];
                    
                    danweiLabel.text=[NSString stringWithFormat:@"%d日%d时",dday,dhour];
                    [unitView addSubview:danweiLabel];
                    
                    [danweiLabel release];
                }
                
                
                NSString *yearKey = [NSString stringWithFormat:@"year%d", dyear];
                NSMutableDictionary *yearDict = [self.timelineDataDict objectForKey:yearKey];
                if (yearDict != nil) {
                    
                    NSString *monthKey = [NSString stringWithFormat:@"month%d", dmonth];
                    if (dmonth<10) {
                        monthKey=[NSString stringWithFormat:@"month0%d", dmonth];
                    }
                    NSMutableDictionary *monthDict = [yearDict objectForKey:monthKey];
                    if (monthDict != nil) {
                        
                        NSString *dayKey = [NSString stringWithFormat:@"day%d", dday];
                        if (dday<10) {
                            dayKey=[NSString stringWithFormat:@"day0%d", dday];
                        }
                        NSMutableDictionary *dayDict = [monthDict objectForKey:dayKey];
                        if (dayDict != nil) {
                            
                            NSString *hourKey = [NSString stringWithFormat:@"hour%d", dhour];
                            if (dhour<10) {
                                hourKey = [NSString stringWithFormat:@"hour0%d", dhour];
                                
                            }
                            
                            NSMutableDictionary *hourDict = [dayDict objectForKey:hourKey];
                            if (hourDict != nil) {
                                
                                
                                if([[hourDict objectForKey:@"diarys"] count]!=0)   {
                                    
                                    //  unitView.backgroundColor=[UIColor redColor];
                                    [unitView setUserInteractionEnabled:YES];
                                    [unitView setMultipleTouchEnabled:YES];
                                    //UIButton *button=[[UIButton alloc]initWithFrame:CGRectMake(0, 0, 50, 40)];
                                    
                                    UIButton *button=[[UIButton alloc]initWithFrame:CGRectMake(unitView.frame.origin.x-unitView.frame.size.width/4, unitView.frame.origin.y, unitView.frame.size.width/2, unitView.frame.size.height)];
                                    
                                    button.tag=[[[[hourDict objectForKey:@"diarys"] objectAtIndex:0] objectForKey:@"id"] intValue];
                                    
                                    
                                    button.userInteractionEnabled=YES;
                                    [button addTarget:self action:@selector(toDiaryView:) forControlEvents:UIControlEventTouchUpInside];
                                    [button setBackgroundColor:[UIColor clearColor]];
                                    UIImageView *poitImageView = [[UIImageView alloc] initWithFrame:CGRectMake(-2, -4, 6, 6)];
                                    [poitImageView setImage:[UIImage imageNamed:@"point_3.png"]];
                                    // [button addSubview:poitImageView];
                                    [unitView.lineView addSubview:poitImageView];
                                    [poitImageView release];
                                    
                                    
                                    [button setCenter:CGPointMake(10*i*scrolValue, (100 -height) /2)];
                                    [myScrollView addSubview:button];
                                    [scrollSubViews setObject:button forKey:[NSString stringWithFormat:@"button%d",i]];
                                    [button release];
                                    
                                    
                                }
                                
                            }
                            
                            
                        }
                        
                        
                    }
                    
                    
                }
                NSString *unitViewXPosition = [NSString stringWithFormat:@"unit%d", i];
                
                [scrollSubViews setObject:unitView forKey:unitViewXPosition];
            }//category
            
            
            if(unitCategory==@"day")
            {
                
                if (i %5 == 0) {
                    height = 45;
                }
                else{
                    height = 30;
                }
                UnitView *unitView = [[UnitView alloc] initWithFrame:CGRectMake(0, 0, unitViewWidth*scrolValue, height)];
                [unitView setCenter:CGPointMake(10*i*scrolValue, (100 -height) /2)];
                [scrollBaseView addSubview:unitView];
                
                
                dday=30-i%30;
                if (dday==0) {
                    dday=30;
                }
                
                dmonth=abs(curMonth-i/30)%12;
                if (dmonth==0) {
                    dmonth=12;
                }
                if (curMonth-i/30<0) {
                    dmonth=12-abs(curMonth-i/30)%12;
                }
                
                dyear=curYear-i/360;
                if (i%30 == 0) {  //a month later
                    //刷新数据 
                    
                    begindate=begindate-86400000*30; 
                    
                    enddate = enddate-86400000*30; 
                    
                    NSString *enddateStr = [NSString stringWithFormat:@"%lld", enddate];
                    NSString *begindateStr = [NSString stringWithFormat:@"%lld", begindate];
                    
                    [self loadRulerDataWith:begindateStr endtime:enddateStr];
                }
                
                
                if (i%5==0) {
                    UILabel *danweiLabel=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 80, 20)];
                      [danweiLabel setTextColor:[UIColor lightGrayColor]];
                    [danweiLabel setTextAlignment:UITextAlignmentCenter];
                    [danweiLabel setBackgroundColor:[UIColor clearColor]];
                    [danweiLabel.text sizeWithFont:[UIFont systemFontOfSize:1]];
                    
                    danweiLabel.text=[NSString stringWithFormat:@"%d月%d日",dmonth,dday];
                    
                    [unitView addSubview:danweiLabel];
                    
                    [danweiLabel release];
                }
                
                NSString *yearKey = [NSString stringWithFormat:@"year%d", dyear];
                NSMutableDictionary *yearDict = [self.timelineDataDict objectForKey:yearKey];
                if (yearDict != nil) {
                    
                    NSString *monthKey = [NSString stringWithFormat:@"month%d", dmonth];
                    if (dmonth<10) {
                        monthKey=[NSString stringWithFormat:@"month0%d", dmonth];
                    }
                    NSMutableDictionary *monthDict = [yearDict objectForKey:monthKey];
                    if (monthDict != nil) {
                        
                        NSString *dayKey = [NSString stringWithFormat:@"day%d", dday];
                        if (dday<10) {
                            dayKey=[NSString stringWithFormat:@"day0%d", dday];
                        }
                        NSMutableDictionary *dayDict = [monthDict objectForKey:dayKey];
                        if (dayDict != nil) {
                            
                            
                            if([[dayDict objectForKey:@"diarys"] count]!=0)   {
                                
                                //unitView.backgroundColor=[UIColor redColor];
                                [unitView setUserInteractionEnabled:YES];
                                [unitView setMultipleTouchEnabled:YES];
                                //UIButton *button=[[UIButton alloc]initWithFrame:CGRectMake(0, 0, 50, 40)];
                                
                                UIButton *button=[[UIButton alloc]initWithFrame:CGRectMake(unitView.frame.origin.x-unitView.frame.size.width/4, unitView.frame.origin.y, unitView.frame.size.width/2, unitView.frame.size.height)];
                                
                                button.tag=[[[[dayDict objectForKey:@"diarys"] objectAtIndex:0] objectForKey:@"id"] intValue];
                                
                                
                                button.userInteractionEnabled=YES;
                                [button addTarget:self action:@selector(toDiaryView:) forControlEvents:UIControlEventTouchUpInside];
                                [button setBackgroundColor:[UIColor clearColor]];
                                UIImageView *poitImageView = [[UIImageView alloc] initWithFrame:CGRectMake(-2, -4, 6, 6)];
                                [poitImageView setImage:[UIImage imageNamed:@"point_3.png"]];
                                // [button addSubview:poitImageView];
                                [unitView.lineView addSubview:poitImageView];
                                [poitImageView release];
                                
                                [button setCenter:CGPointMake(10*i*scrolValue, (100 -height) /2)];
                                [myScrollView addSubview:button];
                                [scrollSubViews setObject:button forKey:[NSString stringWithFormat:@"button%d",i]];
                                [button release];
                                
                                
                            }
                            
                        }
                        
                        
                    }
                    
                    
                }
                NSString *unitViewXPosition = [NSString stringWithFormat:@"unit%d", i];
                
                [scrollSubViews setObject:unitView forKey:unitViewXPosition];
            }//category
            
            if(unitCategory==@"month")
            {
                if (i %3 == 0) {
                    height = 45;
                }
                else{
                    height = 30;
                }
                UnitView *unitView = [[UnitView alloc] initWithFrame:CGRectMake(0, 0, unitViewWidth*scrolValue, height)];
                [unitView setCenter:CGPointMake(10*i*scrolValue, (100 -height) /2)];
                [scrollBaseView addSubview:unitView];
                
                dmonth=12-i%12;
                if (dmonth==0) {
                    dmonth=12;
                }
                if (i<0) {
                    dmonth=12-abs(i)%12;
                }
                dyear=curYear-i/12;
                
                if (i%30==0) {    // 30 month later
                    //刷新数据
                    begindate=begindate-86400000*30*30; 
                    
                    enddate = enddate-86400000*30*30; 
                    
                    NSString *enddateStr = [NSString stringWithFormat:@"%lld", enddate];
                    NSString *begindateStr = [NSString stringWithFormat:@"%lld", begindate];
                    
                    [self loadRulerDataWith:begindateStr endtime:enddateStr];
                    
                }
                
                
                if (i%3==0) {
                    UILabel *danweiLabel=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 20)];
                      [danweiLabel setTextColor:[UIColor lightGrayColor]];
                    [danweiLabel setTextAlignment:UITextAlignmentLeft];
                    [danweiLabel setBackgroundColor:[UIColor clearColor]];
                    [danweiLabel.text sizeWithFont:[UIFont systemFontOfSize:1]];
                    
                    danweiLabel.text=[NSString stringWithFormat:@"%d年%d月",dyear,dmonth];
                    // [danweiLabel setCenter:CGPointMake(10*i*scrolValue, 10)];
                    [unitView addSubview:danweiLabel];
                    
                    [danweiLabel release];
                }
                
                
                NSString *yearKey = [NSString stringWithFormat:@"year%d", dyear];
                NSMutableDictionary *yearDict = [self.timelineDataDict objectForKey:yearKey];
                if (yearDict != nil) {
                    
                    NSString *monthKey = [NSString stringWithFormat:@"month%d", dmonth];
                    if (dmonth<10) {
                        monthKey=[NSString stringWithFormat:@"month0%d", dmonth];
                    }
                    NSMutableDictionary *monthDict = [yearDict objectForKey:monthKey];
                    if (monthDict != nil) {
                        
                        
                        //unitView.backgroundColor=[UIColor redColor];
                        [unitView setUserInteractionEnabled:YES];
                        [unitView setMultipleTouchEnabled:YES];
                        //UIButton *button=[[UIButton alloc]initWithFrame:CGRectMake(0, 0, 50, 40)];
                        
                        UIButton *button=[[UIButton alloc]initWithFrame:CGRectMake(unitView.frame.origin.x-unitView.frame.size.width/4, unitView.frame.origin.y, unitView.frame.size.width/2, unitView.frame.size.height)];
                        
                        button.tag=[[[[monthDict objectForKey:@"diarys"] objectAtIndex:0] objectForKey:@"id"] intValue];
                        
                        
                        button.userInteractionEnabled=YES;
                        [button addTarget:self action:@selector(toDiaryView:) forControlEvents:UIControlEventTouchUpInside];
                        [button setBackgroundColor:[UIColor clearColor]];
                        UIImageView *poitImageView = [[UIImageView alloc] initWithFrame:CGRectMake(-2, -4, 6, 6)];
                        [poitImageView setImage:[UIImage imageNamed:@"point_3.png"]];
                        // [button addSubview:poitImageView];
                        [unitView.lineView addSubview:poitImageView];
                        [poitImageView release];
                        
                        
                        [button setCenter:CGPointMake(10*i*scrolValue, (100 -height) /2)];
                        [myScrollView addSubview:button];
                        
                        [scrollSubViews setObject:button forKey:[NSString stringWithFormat:@"button%d",i]];
                        [button release];
                        
                        
                    }
                    
                    
                }
                NSString *unitViewXPosition = [NSString stringWithFormat:@"unit%d", i];
                
                [scrollSubViews setObject:unitView forKey:unitViewXPosition];
            }
            
            CLog(@"i %d", i);
            CLog(@"dyear%d dmonth%d dday%d dhour%d dmin%d", dyear,dmonth,dday,dhour,dminute);
            
        }
        indexUp = newIndexUP;
        
    }
    //是否超出当前显示下限
    if (newIndexDown < indexDown) {
        
        
        for (int i = indexDown -1; i >= newIndexDown; i--) {
            
            if(unitCategory==@"min"){
                
                if (i %5 == 0) {
                    height = 45;
                }
                else{
                    height = 30;
                }
                UnitView *unitView = [[UnitView alloc] initWithFrame:CGRectMake(0, 0, unitViewWidth*scrolValue, height)];
                [unitView setCenter:CGPointMake(10*i*scrolValue, (100 -height) /2)];
                [scrollBaseView addSubview:unitView];
                
                
                dminute=60-i%60;
                if (dminute==60) {
                    dminute=0;
                }
                
                dhour=23-((i-1)/60)%24;
                
                dday=abs(self.curDay-i/1440)%30;
                if (dday==0) {
                    dday=30;
                }
                
                if (self.curDay-i/1440<0) {
                    dday=30-abs(self.curDay-i/1440)%30;
                }
                
                dmonth=abs(curMonth-i/43200)%12;
                if (dmonth==0) {
                    dmonth=12;
                }
                
                if (i<=0) {
                    dminute=(abs(i)%60);
                    dhour=(abs(i)/60%24);
                    dday=abs(self.curDay+1-abs(i)/1440)%30;
                    dmonth=abs(curMonth+1-i/43200)%12;
                }
                
                if (curMonth-i/43200<0) {
                    dmonth=12-abs(curMonth-i/43200)%12;
                }
                
                
                
                if (i%518400==0) {
                    dyear=curYear-i/518400;
                }
                
                
                if (i%10==0) {
                    UILabel *danweiLabel=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 80, 20)];
                      [danweiLabel setTextColor:[UIColor lightGrayColor]];
                    [danweiLabel setTextAlignment:UITextAlignmentLeft];
                    [danweiLabel setBackgroundColor:[UIColor clearColor]];
                    [danweiLabel.text sizeWithFont:[UIFont systemFontOfSize:4]];
                    
                    danweiLabel.text=[NSString stringWithFormat:@"%d:%d",dhour,dminute];
                    
                    [unitView addSubview:danweiLabel];
                    
                    [danweiLabel release];
                }
                
                
                
                
                NSString *yearKey = [NSString stringWithFormat:@"year%d", dyear];
                NSMutableDictionary *yearDict = [self.timelineDataDict objectForKey:yearKey];
                if (yearDict != nil) {
                    
                    NSString *monthKey = [NSString stringWithFormat:@"month%d", dmonth];
                    if (dmonth<10) {
                        monthKey=[NSString stringWithFormat:@"month0%d", dmonth];
                    }
                    NSMutableDictionary *monthDict = [yearDict objectForKey:monthKey];
                    if (monthDict != nil) {
                        
                        NSString *dayKey = [NSString stringWithFormat:@"day%d", dday];
                        if (dday<10) {
                            dayKey=[NSString stringWithFormat:@"day0%d", dday];
                        }
                        NSMutableDictionary *dayDict = [monthDict objectForKey:dayKey];
                        if (dayDict != nil) {
                            
                            NSString *hourKey = [NSString stringWithFormat:@"hour%d", dhour];
                            if (dhour<10) {
                                hourKey = [NSString stringWithFormat:@"hour0%d", dhour];
                                
                            }
                            
                            NSMutableDictionary *hourDict = [dayDict objectForKey:hourKey];
                            if (hourDict != nil) {
                                
                                NSString *minKey = [NSString stringWithFormat:@"min%d", dminute];
                                if (i<10) {
                                    minKey=[NSString stringWithFormat:@"min0%d", dminute];
                                }
                                NSMutableDictionary *minDict = [hourDict objectForKey:minKey];
                                if (minDict != nil) {
                                    if([[minDict objectForKey:@"diarys"] count]!=0)   {
                                        
                                        //unitView.backgroundColor=[UIColor redColor];
                                        [unitView setUserInteractionEnabled:YES];
                                        [unitView setMultipleTouchEnabled:YES];
                                        //UIButton *button=[[UIButton alloc]initWithFrame:CGRectMake(0, 0, 50, 40)];
                                        
                                        UIButton *button=[[UIButton alloc]initWithFrame:CGRectMake(unitView.frame.origin.x-unitView.frame.size.width/4, unitView.frame.origin.y, unitView.frame.size.width/2, unitView.frame.size.height)];
                                        
                                        button.tag=[[[[minDict objectForKey:@"diarys"] objectAtIndex:0] objectForKey:@"id"] intValue];
                                        
                                        
                                        button.userInteractionEnabled=YES;
                                        [button addTarget:self action:@selector(toDiaryView:) forControlEvents:UIControlEventTouchUpInside];
                                        [button setBackgroundColor:[UIColor clearColor]];
                                        UIImageView *poitImageView = [[UIImageView alloc] initWithFrame:CGRectMake(-2, -4, 6, 6)];
                                        [poitImageView setImage:[UIImage imageNamed:@"point_3.png"]];
                                        // [button addSubview:poitImageView];
                                        [unitView.lineView addSubview:poitImageView];                                        [poitImageView release];
                                        
                                        
                                        [button setCenter:CGPointMake(10*i*scrolValue, (100 -height) /2)];
                                        [myScrollView addSubview:button];
                                        [scrollSubViews setObject:button forKey:[NSString stringWithFormat:@"button%d",i]];
                                        [button release];
                                        
                                        
                                    }
                                    
                                }
                                
                            }
                            
                            
                        }
                        
                        
                    }
                    
                    
                }
                NSString *unitViewXPosition = [NSString stringWithFormat:@"unit%d", i];
                
                [scrollSubViews setObject:unitView forKey:unitViewXPosition];
            }//category
            
            if(unitCategory==@"hour")
            {
                
                
                if (i %3 == 0) {
                    height = 45;
                }
                else{
                    height = 30;
                }
                UnitView *unitView = [[UnitView alloc] initWithFrame:CGRectMake(0, 0, unitViewWidth*scrolValue, height)];
                [unitView setCenter:CGPointMake(10*i*scrolValue, (100 -height) /2)];
                [scrollBaseView addSubview:unitView];
                
                dhour=24-i%24;
                if (dhour==24) {
                    dhour=0;
                }
                
                self.dday=abs(curDay-(i-1)/24)%30;
                if (dday==0) {
                    dday=30;
                }
                if (curDay-i/24<0) {
                    dday=30-abs(curDay-i/24)%30;
                }
                
                if (i<=0) {
                    dhour=abs(i)%24;
                    self.dday=abs(curDay+1-i/24)%30;
                }              
                
                dmonth=abs(curMonth-i/720)%12;
                if (dmonth==0) {
                    dmonth=12;
                    if (curMonth-i/720<0) {
                        dmonth=12-abs(curMonth-i/720)%12;
                    }
                    
                }
                
                dyear=curYear-i/8640;
                
                
                if (i%6==0) {
                    UILabel *danweiLabel=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 80, 20)];
                      [danweiLabel setTextColor:[UIColor lightGrayColor]];
                    [danweiLabel setTextAlignment:UITextAlignmentCenter];
                    [danweiLabel setBackgroundColor:[UIColor clearColor]];
                    [danweiLabel.text sizeWithFont:[UIFont systemFontOfSize:3]];
                    
                    danweiLabel.text=[NSString stringWithFormat:@"%d日%d时",self.dday,dhour];
                    [unitView addSubview:danweiLabel];
                    
                    [danweiLabel release];
                }
                
                
                NSString *yearKey = [NSString stringWithFormat:@"year%d", dyear];
                NSMutableDictionary *yearDict = [self.timelineDataDict objectForKey:yearKey];
                if (yearDict != nil) {
                    
                    NSString *monthKey = [NSString stringWithFormat:@"month%d", dmonth];
                    if (dmonth<10) {
                        monthKey=[NSString stringWithFormat:@"month0%d", dmonth];
                    }
                    NSMutableDictionary *monthDict = [yearDict objectForKey:monthKey];
                    if (monthDict != nil) {
                        
                        NSString *dayKey = [NSString stringWithFormat:@"day%d", dday];
                        if (dday<10) {
                            dayKey=[NSString stringWithFormat:@"day0%d", dday];
                        }
                        NSMutableDictionary *dayDict = [monthDict objectForKey:dayKey];
                        if (dayDict != nil) {
                            
                            NSString *hourKey = [NSString stringWithFormat:@"hour%d", dhour];
                            if (dhour<10) {
                                hourKey = [NSString stringWithFormat:@"hour0%d", dhour];
                                
                            }
                            
                            NSMutableDictionary *hourDict = [dayDict objectForKey:hourKey];
                            if (hourDict != nil) {
                                
                                
                                if([[hourDict objectForKey:@"diarys"] count]!=0)   {
                                    
                                    //unitView.backgroundColor=[UIColor redColor];
                                    [unitView setUserInteractionEnabled:YES];
                                    [unitView setMultipleTouchEnabled:YES];
                                    //UIButton *button=[[UIButton alloc]initWithFrame:CGRectMake(0, 0, 50, 40)];
                                    
                                    UIButton *button=[[UIButton alloc]initWithFrame:CGRectMake(unitView.frame.origin.x-unitView.frame.size.width/4, unitView.frame.origin.y, unitView.frame.size.width/2, unitView.frame.size.height)];
                                    
                                    button.tag=[[[[hourDict objectForKey:@"diarys"] objectAtIndex:0] objectForKey:@"id"] intValue];
                                    
                                    
                                    button.userInteractionEnabled=YES;
                                    [button addTarget:self action:@selector(toDiaryView:) forControlEvents:UIControlEventTouchUpInside];
                                    [button setBackgroundColor:[UIColor clearColor]];
                                    UIImageView *poitImageView = [[UIImageView alloc] initWithFrame:CGRectMake(-2, -4, 6, 6)];
                                    [poitImageView setImage:[UIImage imageNamed:@"point_3.png"]];
                                    // [button addSubview:poitImageView];
                                    [unitView.lineView addSubview:poitImageView];
                                    [poitImageView release];
                                    
                                    [button setCenter:CGPointMake(10*i*scrolValue, (100 -height) /2)];
                                    [myScrollView addSubview:button];
                                    [scrollSubViews setObject:button forKey:[NSString stringWithFormat:@"button%d",i]];
                                    [button release];
                                    
                                    
                                }
                                
                            }
                            
                            
                        }
                        
                        
                    }
                    
                    
                }
                NSString *unitViewXPosition = [NSString stringWithFormat:@"unit%d", i];
                
                [scrollSubViews setObject:unitView forKey:unitViewXPosition];
            }//category
            
            
            if(unitCategory==@"day")
            {
                
                if (i %5 == 0) {
                    height = 45;
                }
                else{
                    height = 30;
                }
                UnitView *unitView = [[UnitView alloc] initWithFrame:CGRectMake(0, 0, unitViewWidth*scrolValue, height)];
                [unitView setCenter:CGPointMake(10*i*scrolValue, (100 -height) /2)];
                [scrollBaseView addSubview:unitView];
                
                
                dday=30-i%30;
                if (dday==0) {
                    dday=30;
                }
                
                
                
                dmonth=abs(curMonth-i/30)%12;
                if (dmonth==0) {
                    dmonth=12;
                }
                if (curMonth-i/30<0) {
                    dmonth=12-abs(curMonth-i/30)%12;
                }
                
                
                dyear=curYear-i/360;
                
                if (i<0) {
                    
                    dmonth=abs(curMonth+1-i/30)%12;
                    
                }
                if (i==0) {
                    dmonth=curMonth;
                }
                
                
                
                if (i%5==0) {
                    UILabel *danweiLabel=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 80, 20)];
                      [danweiLabel setTextColor:[UIColor lightGrayColor]];
                    [danweiLabel setTextAlignment:UITextAlignmentCenter];
                    [danweiLabel setBackgroundColor:[UIColor clearColor]];
                    [danweiLabel.text sizeWithFont:[UIFont systemFontOfSize:1]];
                    
                    danweiLabel.text=[NSString stringWithFormat:@"%d月%d日",dmonth,dday];
                    
                    [unitView addSubview:danweiLabel];
                    [danweiLabel release];
                }
                
                
                
                
                NSString *yearKey = [NSString stringWithFormat:@"year%d", dyear];
                NSMutableDictionary *yearDict = [self.timelineDataDict objectForKey:yearKey];
                if (yearDict != nil) {
                    
                    NSString *monthKey = [NSString stringWithFormat:@"month%d", dmonth];
                    if (dmonth<10) {
                        monthKey=[NSString stringWithFormat:@"month0%d", dmonth];
                    }
                    NSMutableDictionary *monthDict = [yearDict objectForKey:monthKey];
                    if (monthDict != nil) {
                        
                        NSString *dayKey = [NSString stringWithFormat:@"day%d", dday];
                        if (dday<10) {
                            dayKey=[NSString stringWithFormat:@"day0%d", dday];
                        }
                        NSMutableDictionary *dayDict = [monthDict objectForKey:dayKey];
                        if (dayDict != nil) {
                            
                            
                            if([[dayDict objectForKey:@"diarys"] count]!=0)   {
                                
                                // unitView.backgroundColor=[UIColor redColor];
                                [unitView setUserInteractionEnabled:YES];
                                [unitView setMultipleTouchEnabled:YES];
                                //UIButton *button=[[UIButton alloc]initWithFrame:CGRectMake(0, 0, 50, 40)];
                                
                                UIButton *button=[[UIButton alloc]initWithFrame:CGRectMake(unitView.frame.origin.x-unitView.frame.size.width/4, unitView.frame.origin.y, unitView.frame.size.width/2, unitView.frame.size.height)];
                                
                                button.tag=[[[[dayDict objectForKey:@"diarys"] objectAtIndex:0] objectForKey:@"id"] intValue];
                                
                                
                                button.userInteractionEnabled=YES;
                                [button addTarget:self action:@selector(toDiaryView:) forControlEvents:UIControlEventTouchUpInside];
                                [button setBackgroundColor:[UIColor clearColor]];
                                UIImageView *poitImageView = [[UIImageView alloc] initWithFrame:CGRectMake(-2, -4, 6, 6)];
                                [poitImageView setImage:[UIImage imageNamed:@"point_3.png"]];
                                // [button addSubview:poitImageView];
                                [unitView.lineView addSubview:poitImageView];                                [poitImageView release];
                                
                                
                                [button setCenter:CGPointMake((unitViewWidth/2)*i*scrolValue, (100 -height) /2)];
                                [myScrollView addSubview:button];
                                [scrollSubViews setObject:button forKey:[NSString stringWithFormat:@"button%d",i]];
                                [button release];
                                
                                
                            }
                            
                            
                            
                            
                        }
                        
                        
                    }
                    
                    
                }
                NSString *unitViewXPosition = [NSString stringWithFormat:@"unit%d", i];
                
                [scrollSubViews setObject:unitView forKey:unitViewXPosition];
            }//category
            
            if(unitCategory==@"month")
            {
                if (i %3 == 0) {
                    height = 45;
                }
                else{
                    height = 30;
                }
                UnitView *unitView = [[UnitView alloc] initWithFrame:CGRectMake(0, 0, unitViewWidth*scrolValue, height)];
                [unitView setCenter:CGPointMake(10*i*scrolValue, (100 -height) /2)];
                [scrollBaseView addSubview:unitView];
                
                dmonth=12-i%12;
                if (dmonth==0) {
                    dmonth=12;
                }
                
                dyear=curYear-i/12;
                
                if (i==0) {
                    dmonth=12;
                    dyear=curYear;
                }
                
                if (i<0) {
                    dmonth=abs(i)%12;
                    dyear=curYear+1-i/12;
                }
                
                
                
                if (i%3==0) {
                    UILabel *danweiLabel=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 20)];
                      [danweiLabel setTextColor:[UIColor lightGrayColor]];
                    [danweiLabel setTextAlignment:UITextAlignmentLeft];
                    [danweiLabel setBackgroundColor:[UIColor clearColor]];
                    [danweiLabel.text sizeWithFont:[UIFont systemFontOfSize:1]];
                    
                    danweiLabel.text=[NSString stringWithFormat:@"%d年%d月",dyear,dmonth];
                    [unitView addSubview:danweiLabel];
                    
                    [danweiLabel release];
                }
                
                
                
                NSString *yearKey = [NSString stringWithFormat:@"year%d", dyear];
                NSMutableDictionary *yearDict = [self.timelineDataDict objectForKey:yearKey];
                if (yearDict != nil) {
                    
                    NSString *monthKey = [NSString stringWithFormat:@"month%d", dmonth];
                    if (dmonth<10) {
                        monthKey=[NSString stringWithFormat:@"month0%d", dmonth];
                    }
                    NSMutableDictionary *monthDict = [yearDict objectForKey:monthKey];
                    if (monthDict != nil) {
                        
                        
                        //unitView.backgroundColor=[UIColor redColor];
                        [unitView setUserInteractionEnabled:YES];
                        [unitView setMultipleTouchEnabled:YES];
                        //UIButton *button=[[UIButton alloc]initWithFrame:CGRectMake(0, 0, 50, 40)];
                        
                        UIButton *button=[[UIButton alloc]initWithFrame:CGRectMake(unitView.frame.origin.x-unitView.frame.size.width/4, unitView.frame.origin.y, unitView.frame.size.width/2, unitView.frame.size.height)];
                        
                        button.tag=[[[[monthDict objectForKey:@"diarys"] objectAtIndex:0] objectForKey:@"id"] intValue];
                        
                        
                        button.userInteractionEnabled=YES;
                        [button addTarget:self action:@selector(toDiaryView:) forControlEvents:UIControlEventTouchUpInside];
                        [button setBackgroundColor:[UIColor clearColor]];
                        UIImageView *poitImageView = [[UIImageView alloc] initWithFrame:CGRectMake(-2, -4, 6, 6)];
                        [poitImageView setImage:[UIImage imageNamed:@"point_3.png"]];
                        // [button addSubview:poitImageView];
                        [unitView.lineView addSubview:poitImageView];
                        [poitImageView release];
                        
                        
                        [button setCenter:CGPointMake((unitViewWidth/2)*i*scrolValue, (100 -height) /2)];
                        [myScrollView addSubview:button];
                        
                        [scrollSubViews setObject:button forKey:[NSString stringWithFormat:@"button%d",i]];
                        [button release];
                        
                        
                        
                        
                        
                    }
                    
                    
                }
                NSString *unitViewXPosition = [NSString stringWithFormat:@"unit%d", i];
                
                [scrollSubViews setObject:unitView forKey:unitViewXPosition];
            }
            
            
            CLog(@"i %d", i);
            CLog(@"dyear%d dmonth%d dday%d dhour%d dmin%d", dyear,dmonth,dday,dhour,dminute);
            
            
        }
        indexDown = newIndexDown;
        
    }
    
    //  NSLog(@"scroll view center %f,%f",pointX, pointY);
    
    [scrollView setContentOffset:CGPointMake(pointX, pointY) animated:YES];
    
    //重设ContentSize
    float newContentSize = pointX +640;
    CGSize size = scrollView.contentSize;
    if (newContentSize > size.width) {
        
        [scrollView setContentSize:CGSizeMake(newContentSize, size.height)];
    }
    
    //对已超出范围的view 移除
    if (newIndexUP <indexUp) {
        for (int i = newIndexUP +1; i <=indexUp; i++) {
            NSString *unitViewXPosition = [NSString stringWithFormat:@"unit%d", i];
            
            UIView *unitView = [scrollSubViews objectForKey:unitViewXPosition];
            
            [unitView removeFromSuperview];
            
            
            NSString *buttonViewXPosition = [NSString stringWithFormat:@"button%d", i];
            
            UIView *button = [scrollSubViews objectForKey:buttonViewXPosition];
            
            [button removeFromSuperview];
        }
        
        indexUp = newIndexUP;
    }
    
    if (newIndexDown >indexDown) {
        for (int i = newIndexDown -1; i >=indexDown; i--) {
            NSString *unitViewXPosition = [NSString stringWithFormat:@"unit%d", i];
            
            UIView *unitView = [scrollSubViews objectForKey:unitViewXPosition];
            
            [unitView removeFromSuperview];
            
            NSString *buttonViewXPosition = [NSString stringWithFormat:@"button%d", i];
            
            UIView *button = [scrollSubViews objectForKey:buttonViewXPosition];
            
            [button removeFromSuperview];
            
        }
        
        indexDown = newIndexDown;
    }
    
    
    //reload 尺子数据
    [self reloadRulerData];

    
   // NSLog(@"Scroll View did end decelerating!");
}

- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView
{
   // NSLog(@"Scroll View begin decelerating!");
}// called on finger up as we are moving

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{

       
}
// called when scroll view grinds to a halt

- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
    
    //reload 尺子数据
   // [self reloadRulerData];

    
   // NSLog(@"Scroll View did end scrolling animation!");
}// called when setContentOffset/scrollRectVisible:animated: finishes. not called if not animating

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
     //NSLog(@"Scroll View zooming in scroll view! %f", scrollView.zoomScale);
    //return [scrollView viewWithTag:ZOOM_VIEW_TAG];
    return [scrollView viewWithTag:111];
   // NSLog(@"Scroll View zooming in scroll view!");
}// return a view that will be scaled. if delegate returns nil, nothing happens

- (void)scrollViewWillBeginZooming:(UIScrollView *)scrollView withView:(UIView *)view __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_3_2)
{
   // NSLog(@"Scroll View begin zooming !");
}// called before the scroll view begins zooming its content
//- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale
//{
////    [scrollView setZoomScale:scale+0.01 animated:NO];
////    [scrollView setZoomScale:scale animated:NO];
//    
//    //scrolValue = scrollView.zoomScale;
//    NSLog(@"Scroll View did end zooming!");
//}// scale between minimum and maximum. called after any 'bounce' animations

- (BOOL)scrollViewShouldScrollToTop:(UIScrollView *)scrollView
{
   // NSLog(@"Scroll View should scroll to top!");
    
    return YES;
}// return a yes if you want to scroll to the top. if not defined, assumes YES

- (void)scrollViewDidScrollToTop:(UIScrollView *)scrollView
{
   // NSLog(@"Scroll View did scroll to top!");
}// called when scrolling animation finished. may be called immediately if already at top


                         
@end
